JSTL forTokens Example

JSTL forTokens Example explains about how to use JSTL forTokens inside JSP and its various mandatory and optional attributes.

c:forTokens Tag is used for splitting a string into tokens and iterate through each of the tokens, c:forTokens Tag is available as a part of JSTL core tags

Inside c:forTokens Tag, you need to specify following mandatory attributes.

items -> These values (token strings) are used to iterate with delimiters.

delims -> These values are that split / separate the token string

Inside c:forTokens Tag, you need to specify following optional attributes.

begin -> Starting element from

end -> Ending element 

step -> Iterating steps

var -> Current item

varStatus -> Name of the variable inside loop

You can see the below example demonstrating the usage JSTL c:forTokens Tag Example inside JSTL

Required Libraries

You need to download

  1. Tomcat 7
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

JSTL forTokens Example

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
    <head>
        <title> JSTL c:forTokens Example</title>
    </head>
    <body>
		
        <!-- JSTL forTokens tag for iterating over comma separated Strings -->
        <c:forTokens var="token" items="Struts, Spring, Hibernate, EJB" delims=",">
	  <c:out value="${token}"/> </br>
        </c:forTokens>

	<!-- JSTL forTokens tag for iterating over comma separated Numbers -->
        <c:forTokens delims="," items="10,15,20,25" var="number">
	  <c:out value="${number}" default="25"></c:out>
	</c:forTokens>

	<!-- JSTL forTokens tag looping numbers from 1 to 10 with step -->
	<c:forTokens var="item" begin="0" end="10" step="2">
	  <c:out value="${item}"/><p>
	</c:forTokens>

    </body>
</html>
Output
Struts
Spring
Hibernate
EJB

10
15
20
25

2
4
6
8
10

 






Your email address will not be published. Required fields are marked *