In this JSTL tutorial, We will see how to iterate data using for and forEach loop in JSTL also will continue one hand-on example to Iterate tokens separated by a delimiter.
How to use for-Loop in JSTL
JSTL library contains a tag <c:forEach></c:forEach> that is used to implement for-loop. but did you notice the tag is forEach not for?
Yes, It is a forEach loop but using attributes we can perform operations similar to for loop. in the below code, We are trying to print numbers from 1 to 10. so let’s understand the working each attributes that are used under the tag <c:forEach></c:forEach>
var=”i” is defined as a variable with the name “i”.
begin=”1″ is used to initialize a starting point of the loop.
end=”10″ is used to initialize an endpoint of the loop.
Note: By default value will be incremented by 1 in each interaction
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h2>For and ForEach loop in JSTL</h2>
<c:forEach var="i" begin="1" end="10" >
${i}
<br/>
</c:forEach>
</body>
</html>
Can we change the default increment value forEach loop in JSTL?
Yes, we can change the default increment value for each interaction in forEach loop by using the attribute step=”2″. As in the below code the increment will be done by 2.
<c:forEach var="i" begin="1" end="10" step="2">
${i}
<br/>
</c:forEach>
How to use forEach Loop in JSTL
As we have already seen in the above examples <c:forEach></c:forEach> tag is used to define the forEach loop. let’s create a simple example, where we’ll Iterate the element from an array by using a forEach loop in JSTL.
Define an array “users” of String type and add some dummy users’ names. Now implement the forEach loop with attributes items=”<%=users%>” var=”user” where items define the name of the array and var define each element of the array.
Now, print each element by using JSP expression ${user}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h2>For and ForEach loop in JSTL</h2>
<%
String[] users = new String[5];
users[0] = "codebun";
users[1] = "codedec";
users[2] = "bhupi";
users[3] = "bhuvi";
users[4] = "raj";
%>
<c:forEach items="<%=users%>" var="user" >
${user}
<br/>
</c:forEach>
</body>
</html>
Iterate tokens separated by a delimiter in JSTL
Let’s suppose, we have a String and we want to divide it into tokens by using a delimiter. It can be performed easily by using StringTokaniser class but yes, similarly we can perform this into JSTL by using <c:forTokens> </c:forTokens> tag.
In the below code example, “www.codebun.com” is a string, and “.” is the delimiter. Now, define attributes items=”www.codebun.com” delims=”.” var=”tempVar” in <c:forTokens>.
items=”www.codebun.com” is define the String value
delims=”.” is define a delimeter
var=”tempVar” define a variable with the name “tempVar”
Now, print ${tempVar} by using JSP expression. Print it before closing the </c:forTokens> tag.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h2>for Token in JSTL</h2>
<c:forTokens items="www.codebun.com" delims="." var="tempVar">
${tempVar}
<br/>
</c:forTokens>
</body>
</html>