JSTL for Loop

Consider a case where we want to do something with a collection like an array, retrieving one element at a time, and prints that element in a dynamically generated row.

JSTL for Loop

JSTL for Loop <c: forEach> 

        

Consider a case where we want to do something with a collection like an array, retrieving one element at a time, and prints that element in a dynamically generated row. For this  <c: forEach> tag is perfect, this tag gives you the options to iterate over arrays and collections. 

In the tag <c:forEach var="i" begin="1" end="20" step="1" varStatus ="status">. Here this tag is working exactly as, for loop works in a jsp or in java. In this example i is the name of the variable, which begins from 1 and continues up to 20, we are incrementing the variable by 1, to increment the variable we have used step attribute. To print the value of the variable on the browser use <c:out> tag.

 

The code of the program is given below:

 

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>JSTL Simple Conditional Execution Example</title>
</head>
<body bgcolor="#FFFFcc">
<h1>For Loop in JSTL</h1>
<c:forEach var="i" begin="1" end="20" step="1" varStatus ="status">
<c:out value="${i}" /> 
</c:forEach>
</body>
</html>

The output of the program is given below:

Download this example.