JSTL Core Tag forTokens Example

JSTL Core Tag forTokens Example


Posted in : Java Posted on : April 1, 2012 at 7:31 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL flow control Iterator tag forTokens.

JSTL Core Tag forTokens Example

In this tutorial you will learn about the JSTL flow control Iterator tag forTokens.

forTokens tag in JSTL is used for iterating over the collection of objects. forEach tag is also used for this purpose but the purpose of using the forTokens is that it iterates over a collection of tokens, these tokens are separated by a delimiter.

Attributes of <c:forTokens>

  • items : This attribute is used for specifying a collection to iterate over.
  • begin : This attribute is used for specifying the initial item to start from (0= firstItem, 1= secondItem, ....).
  • end    : This attribute is used for specifying the last item, it has a default value as last item (0= firstItem, 1= secondItem, .....)
  • step   : This attribute is used to specifies the process every step items and the default value of this attribute is 1.
  • var    : This attribute is used to specifies the current item
  • varStatus : This attribute is used to specifies the loop status
  • delims : This attribute is used to specifies the characters to be used as delimiters.

Example :

JstlCoreTagForTokensExample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Example</title>
</head>
<body>
<h3>JSTL Core Tag forTokens Example.</h3>
<c:set var="str" value="A!B!C!D" />
Elements are :<br> 
<c:forTokens items="${str}" delims="!" var="elements">
${elements} <br>
</c:forTokens>
</body>
</html>

How to Run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlCoreTagForTokensExample.jsp page of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlCoreTagForTokensExample.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When you will execute the above jsp file an output will displayed on your eclipse browser as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics