Java

JSTL Tags for Condition and Loop

Programmingempire

In this article, I will discuss the examples of JSTL Tags for Condition and Loop. In general JSP tag Library contains <c:if>, and <c:forEach> tags for checking conditions, and loops respectively.

Syntax of <c:if> Tag

<c:if test=””, var=””, scope=””></c:if>

Basically, the test attribute specifies the condition. While the var attribute stores the result of the condition and scope represents the scope of that variable.

Syntax of <c:forEach> Tag

<c:forEach attributes list></c:forEach>

The <c:forEach> contains following attributes.

  • var represents the variable name containing the value of current item
  • items represents the collection of items
  • begin represents the initial value of the variable
  • end represents the final value of the variable
  • step represents the step size
  • varStatus represents the variable indicating loop status

Examples of JSTL Tags for Condition and Loop

The following section contains several examples of JSTL Tags for Condition and Loop.

Find Whether a Given Number is Odd or Even

As can be seen in the following example, the user enters the number. Also, we can retrieve the number using the request.getParameter() method. Further, the value is made available in the JSTL tag using the request.setAttribute() method. Since JSTL doesn’t have a corresponding tag for else, we need to use two separate <c:if> tags.

<%@ 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>Insert title here</title>
</head>
<body>
  <h1>Find whether the number entered by the user is odd or even</h1>
  <form name="f1" method="post" action="LoopAndIfExamples.jsp">
  	 Enter a Number: <input type="text" name="t1"/>
  </form>
  <%
     int x;
     if(request.getParameter("t1")!=null){
     x=Integer.parseInt(request.getParameter("t1"));
     request.setAttribute("attr", x);}
  %>
  <c:if test="${attr%2==0}">
     <c:out value="Even Number" />
  </c:if>
  <c:if test="${attr%2==1}">
     <c:out value="Odd Number" />
  </c:if>
</body>
</html>

Output

Program to Find Odd or Even Number using JSTL
Program to Find Odd or Even Number using JSTL

Find Sum of 10 Numbers

The following code example shows the use of <c:forEach> tag to find the sum of 10 numbers.

<%@ 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>Sum of 10 Numbers</title>
</head>
<body>
    <h1>Find Sum of 10 numbers....</h1>
    <c:set var="sum" value="0"/>
    <c:forEach begin="1" end="10" var="i">
    	<c:set var="sum" value="${sum+i}"/>
    </c:forEach>
    <c:out value="Sum = ${sum}"/>
</body>
</html>

Output

Program to Find Sum of 10 Numbers Using JSTL
Program to Find Sum of 10 Numbers Using JSTL

First 10 Terms of Fibonacci Series

Another example of using<c:forEach> tag is given below. Here we use this tag to display the first 10 terms of the Fibonacci series.

<%@ 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>Sum of 10 Numbers</title>
</head>
<body>
    <h1>First 10 Terms of Fibonacci Series...</h1>
    <c:set var="a" value="-1"/>
    <c:set var="b" value="1"/>
    <c:forEach begin="1" end="10" var="i">
    	<c:set var="c" value="${a+b}"/>
    	<c:out value="${c}  "/>
    	<c:set var="a" value="${b}"/>
    	<c:set var="b" value="${c}"/>
    </c:forEach>
</body>
</html>

Output

Showing First 10 Terms of Fibonacci Series using JSTL
Showing First 10 Terms of Fibonacci Series using JSTL

Display Prime Numbers in Given Range

The following example demonstrates the use of both <c:if>, and <c:forEach> tag to find the prime numbers in a given range.

<%@ 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>Prime Numbers in the Given Range...</title>
</head>
<body>
<h1>Prime Numbers in the Given Range...</h1>
<h2>Range</h2>
<h3>Low: 18, High: 137</h3>
<c:set var="low" value="18"/>
<c:set var="high" value="137"/>
<c:set var="notprime" value="0"/>
<c:forEach begin="${low}" end="${high}" var="i">
	<c:forEach var="j" begin="2" end="${i/2}">
		<c:if test="${i%j==0}">
		   <c:set var="notprime" value="1"/>
		</c:if>
	</c:forEach>
	<c:if test="${notprime==0}">
		<c:out value="${i} "/>
	</c:if>
	<c:set var="notprime" value="0"/>
</c:forEach>
</body>
</html>

Output

Program to Display Prime Numbers in a Given Range using JSTL
Program to Display Prime Numbers in a Given Range using JSTL

Display Sum of Items of a Collection

Besides, iterating a variable a number of times, we can also use the <c:forEach> tag to iterate over a collection of items as shown in the following example.

<%@ 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>Sum of Items</title>
</head>
<body>
<h1>Display Sum of Items of Given Collection</h1>
<h3>Items of Collection...</h3>
<c:set var="sum" value="0"/>
<c:forEach items="7,1,12,9,8,13,35,89,10,25" var="x" >
    <c:set var="sum" value="${sum+x}"/>
    <c:out value="${x}  "/>
</c:forEach>
<br/>
<c:out value="Sum = ${sum}"/>
</body>
</html>

Output

A Program to Display the Sum of Items of a Collection using <c:forEach> JSTL Tag
A Program to Display the Sum of Items of a Collection using <c:forEach> JSTL Tag
programmingempire

You may also like...