Programmingempire
This article discusses the JSTL Tags for Decision Making and Loop. Basically, in Java, we have if and switch…case statements for decision making. Likewise, we have for loop, while loop, and do..while loop in Java.
JSP Tag Library (JSTL) provides several tags for decision-making and loops. While, the c:if tag works like an if statement in Java, there are three tags that provide the functionality of switch…case statement. These tags are c:choose, c:when, and c:otherwise. In order to find an example of using these three tags, click here.
For the purpose of specifying iterations, JSTL provides two tags – c:forEach, and c:forTokens. An example of using c:forTokens is shown here.
c:if and c:forEach JSTL Tags for Decision Making and Loop
At first we discuss the c:if tag of JSTL.
c:if JSTL Tag
In general, c:if tag is used for executing statements on the basis of outcome of a condition. While, there is no corresponding tag for else branch. Hence, if you have some statements to execute for else part, you need to use the c:if tag again with a different condition.
Syntax of c:if Tag
<c:if test="" var="" scope""></c:if>
When this tag is executed, the condition specified by the test is evaluated and the result of the condition is stored in the optionally specified variable in the var attribute. Also, the scope attribute specifies the scope of variable.
c:forEach JSTL Tag
Likewise, c:forEach tag performs iteration a number of times specified by the control variable.
Syntax of c:forEach Tag
<c:forEach items="" var="" begin="" end="" step="" varStatus=""></c:forEach>
Where items attribute represents the collection of items, and var attribute represents loop control variable. Likewise, begin, end, and step attributes represent the starting value, last value of loop control variable, and the step value. Further, the varStatus attribute indicates the status of the variable.
Examples of JSTL Tags for Decision Making and Loop
The following example shows he usage of c:if Tag.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>c:if Examples</title>
</head>
<body>
<c:set var="x" value="556"/>
<c:if test="${x%2==0}">
<c:out value="Even Number!"/>
</c:if>
<c:if test="${x%2==1}">
<c:out value="Odd Number!"/>
</c:if>
</body>
</html>
Output
Likewise, following example demonstrates the use of nested-if using the c:if tag.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>c:if Examples</title>
</head>
<body>
<c:set var="marks" value="76"/>
<c:if test="${marks>40}">
<c:if test="${marks>60}">
<c:if test="${marks>70}">
<c:out value="Marks = ${marks}"/>
<br/>
<c:out value="Grade is B"/>
</c:if>
</c:if>
</c:if>
</body>
</html>
Output
The following example shows the usage of c:forEach loop. As can be seen, the first use of c:forEach tag demonstrates iterating through a loop control variable. While, the second c:forEach tag shows how to iterate over a collection.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>c:forEach Examples</title>
</head>
<body>
Series 1<br/>
<c:forEach var="i" begin="1" end="100" step="7">
<c:out value="${i} "/>
</c:forEach>
<br/>
series 2 (Iterating over a collection)<br/>
<c:forEach items="7, 12, 89, 56, 45, 33, 21" var="i">
<c:out value="${i} "/>
</c:forEach>
</body>
</html>
Output