JSTL forEach Tag <c:forEach>

JSTL forEach tag is used to iterate over a collection of data . It can be Array, List, Set, ArrayList, HashMap or any other collection type.It is commonly use to render a tabular data in our web pages in form of HTML table.A common use of c:forEach is to produce a HTML table containing data gathered from a SQL query or other data source.

JSTL forEach tag is used to iterate over a collection of data . It can be Array, List, Set, ArrayList, HashMap or any other collection type.It is commonly use to render a tabular data in our web pages in form of HTML table.A common use of c:forEach is to produce a HTML table containing data gathered from a SQL query or other data source.



Attributes:

The <c:foreach> tag has following attributes.

NameRequiredTypeDescription
itemsFalsejava.lang.ObjectCollection of items to iterate in the loop.
beginFalseintBegin index of the iteration. Iteration begins at the value mentioned in this attribute value. (if items specified) First item has index of 0.
endFalseintEnd index of the iteration. Iteration stops at the value mentioned in this attribute value (inclusive). (if items specified) .
stepFalseintIteration processes for the step value mentioned in this attribute.
varFalsejava.lang.StringName of the scoped variable which holds the current item in the iteration. This variable’s type depends on the items in the iteration and has nested visibility.
varStatusFalsejava.lang.StringName of the scoped variable which holds the loop status of the current iteration. This variable is of type javax.servlet.jsp.jstl.core.LoopTagStatus and has nested visibility.

Example of <c:foreach> :

<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<html>
<head>
<title>Example c:forEach tag in JSTL</title>
</head>
<body>
	<c:forEach var="counter" begin="1" end="10">
		<c:out value="${counter}" />
	</c:forEach>
</body>
</html>

Output:

jstl forEach


JSTL forEach tag example to iterate Array, List, Map, Map of List, List of Map

Lets see how we can use JSTL to iterate Array, List, Map, Map of List and List of Map with running example.

1 . Array

In the example given below we will be displaying Employee data that are stored as two dimensional array of string. After declaring and initializing the Employee data, we will put these values into the request scope. Later we will use c:forEach tag to iterates it row by row to form an HTML table. Our Employee data consist of the EmpId, Name,PhoneNo and City.

<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<html>
<head>
<title>Employee Details </title>
</head>
<body>
<%
String[][] employeeData = {
{"101", "Mukesh", "123", "Chennai"},
{"102", "Ravi", "258", "Gurgaon"},
{"103", "Amit", "695", "Gurgaon"},
{"104", "Vicky", "897", "Noida"},
{"105", "Golu", "745", "Patna"}
};
request.setAttribute("empdata", employeeData);
%>
Employee Details
<table border="1">
<tr>
<th>EMP ID</th>
<th>Name</th>
<th>Phone</th>
<th>City</th>
</tr>
<c:forEach var="employee" items="${empdata}">
<tr>
<td>${employee[0]}</td>
<td>${employee[1]}</td>
<td align="center">${employee[2]}</td>
<td align="center">${employee[3]}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

Output in Browser :

jstl forEach


2 . List

In the below example will see how to iterate List using JSTL forEach loop. In similar way you can iterate Vector or LinkedList.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core_rt" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%
    ArrayList<String> list = new ArrayList<String>();
    list.add("A");
    list.add("B");
    list.add("C");
    
    request.setAttribute("alist", list);
%>
<html>
<body>
	<c:forEach items="${alist}" var="listItem">
        ${listItem} <br />
	</c:forEach>
</body>
</html>

Output in Browser :

jstl forEach


3 . Map

<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.HashMap"%>
<%
    Map<String,String> map = new HashMap<String,String>();
  
 map.put("key1", "Mitali");
 map.put("key2", "Vicky");
 map.put("key3", "Neil Raj");
 map.put("key4", "Aayush");
 map.put("key5", "Kajol");
 map.put("key3", "Anshu");
 map.put("key3", "Ishant");
 map.put("key3", "Ananya")
    
    request.setAttribute("MyMap", map);
%>
<html>
<body>
    <c:forEach items="${MyMap}" var="mapItem">
        ${mapItem.key} ${mapItem.value} <br/>
    </c:forEach>
</body>
</html>

Output in Browser :

jstl forEach


4 . List of Map

<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.HashMap"%>
<%
    ArrayList<HashMap<String,String>> listOfMap = new ArrayList<HashMap<String,String>>();
  
HashMap<String,String> map;
 
 for(int i=0;i<3;i++){
  map = new HashMap<String, String>();
  
  map.put("key1", "value1"+i);
  map.put("key2", "value2"+i);
  
  listOfMap.add(map);
 }
    
    request.setAttribute("listOfMap", listOfMap);
%>
<html>
<body>
	<c:forEach items="${listOfMap}" var="maps">
		<c:forEach items="${maps}" var="mapItem">
      ${mapItem.key} ${mapItem.value} <br />
		</c:forEach>
	</c:forEach>
</body>
</html>

Output in Browser :

jstl forEach


4 . Map of List

<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.HashMap"%>
<%
    HashMap<String,ArrayList<String>> mapOfList = new HashMap<String,ArrayList<String>>(); 
  
 ArrayList<String> list;
 
 for(int i=0;i<2;i++){
  list = new ArrayList<String>();
  
  list.add("Mitali");
  list.add("Vicky");
  list.add("Neil");
  list.add("Aayush");
  list.add("Kajol");
  list.add("Anshu");
  list.add("Ishant");
  list.add("Ananya");
  
  mapOfList.put("C"+i,list);
 }
    
    request.setAttribute("mapOfList", mapOfList);
%>
<html>
<body>
    <c:forEach items="${mapOfList}" var="list">
        <c:forEach items="${list.value}" var="listItem">
       ${listItem} <br/>
    </c:forEach>
    </c:forEach>
</body>
</html>

Output in Browser :

jstl forEach





comments powered by Disqus