JSTL Core Tag forEach

JSTL Core Tag forEach


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

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

JSTL Core Tag forEach Example

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

 primitive types, implementation of java.util.Iterator, java.util.Enumeration. In supporting of various collection types it supports all implementations of  java.util.Collection and java.util.Map. In <c:forEach> tag a collection is specified by the attribute 'items' and to avail the current item an attribute 'var' is used. In case if you are specifying a collection java.util.Map then java.util.Map.Entry will be the current item that contains the properties 'key' & 'value'. And when you will use the arrays of type primitive such as int, float etc. the standard wrapper class for example, Integer, Float, respectively wraps the current item for iteration automatically.

Attributes of <c:forEach> tag :

  • 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

Example :

Here an example is being given below will demonstrate you how to use <c:forEach> tag in jstl. In this example I have created a List in scriptlets and then set this list into a request.setAttribute(). Then uses the <c:forEach> tag to iterate over this list. So as an output all the elements of list will be displayed.

JstlCoreTagForEachExample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<!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 forEach Example.</h3>
<%
List list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
request.setAttribute("list", list);
%>
Elements list = 
<c:forEach var="i" items="${list}">
${i}
</c:forEach>
</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 JstlCoreTagForEachExample.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 JstlCoreTagForEachExample.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 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