JSTL forEach tag
last modified November 30, 2026
JSTL forEach tutorial shows how to use the forEach tag from the JSTL library.
JSTL
JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags that provide the core functionality common to many JSP applications.
forEach tag
JSTL <c:forEach> tag is a basic iteration tag. It iterates over various Java collection types.
The <c:forEach> tag contains the following attributes:
items— collection of items to iteratebegin— index of the starting itemend— index of the ending itemstep— iteration stepvar— variable for the current item of the iterationvarStatus— object holding the loop status such as the index and count
forEach taglib declaration
The <c:forEach> tag belongs to the core JSTL tags.
<%@ taglib uri="jakarta.tags.core" prefix="c" %>
To use the tag, we need to include this declaration.
JSTL Maven project
The examples in this tutorial are JSP pages in a small Maven web application. The project uses Jakarta Servlet 6.1, JSTL 3.0, and the Jetty 12.1 Maven plugin.
$ tree
.
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── zetcode
│ ├── bean
│ │ └── City.java
│ ├── service
│ │ └── CityService.java
│ └── web
│ └── MyServlet.java
└── webapp
├── index.html
├── languages.html
├── languages.jsp
├── numbers.jsp
├── params.html
├── params.jsp
├── showCities.jsp
└── steps.jsp
The webapp directory contains the JSP pages and the static HTML
pages. The servlet, the service, and the City bean are compiled
into WEB-INF/classes.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zetcode</groupId>
<artifactId>JstlForEach</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>25</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.5.1</version>
</plugin>
<plugin>
<groupId>org.eclipse.jetty.ee11</groupId>
<artifactId>jetty-ee11-maven-plugin</artifactId>
<version>12.1.15</version>
<configuration>
<webApp>
<contextPath>/jstl</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
</project>
The jakarta.servlet-api dependency provides the Servlet API.
The JSTL API and implementation provide the jakarta.tags.core
tags. For mvn jetty:run, the EE11 plugin also supplies JSTL, but
declaring the dependencies explicitly keeps the WAR deployable on other
containers.
forEach tag example
The following JSP page contains the <c:forEach> tag. In addiction to <c:forEach> tag, we also use <c:out> for displaying variables.
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="jakarta.tags.core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Numbers</title>
</head>
<body>
<c:forEach var="counter" begin="1" end="8">
<c:out value="${counter}"/>
</c:forEach>
</body>
</html>
The example shows the values 1 to 8. The begin and
end attributes set the inclusive range, and var
names the variable that holds the current value.
$ curl localhost:8080/jstl/numbers.jsp ... 1 2 3 4 5 6 7 8 ...
forEach tag example II
The next JSP example reads parameters sent from a link.
<!DOCTYPE html>
<html>
<head>
<title>Start Page</title>
<meta charset="UTF-8">
</head>
<body>
<a href="params.jsp?name=Jane&age=23&occupation=accountant">Show page</a>
</body>
</html>
The params.html page contains a link that sends three parameters
to the params.jsp page.
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="jakarta.tags.core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Parameters</title>
</head>
<body>
<c:forEach var="par" items="${param}">
<c:out value="${par.key}"/>: <c:out value="${par.value}"/> <br>
</c:forEach>
</body>
</html>
The JSP page receives parameters in the implicit param object, which is a map.
<c:forEach var="par" items="${param}">
<c:out value="${par.key}"/>: <c:out value="${par.value}"/> <br>
</c:forEach>
We go over the map and print the key/value pairs. The order of the parameters in the map is not guaranteed.
$ curl 'localhost:8080/jstl/params.jsp?name=Jane&age=23' name: Jane age: 23
forEach tag example III
The HTML <select> is a control that provides
a menu of options. With its multiple attribute, the user can select
multiple values form the control.
<!DOCTYPE html>
<html>
<head>
<title>Languages</title>
<meta charset="UTF-8">
</head>
<body>
<form action="languages.jsp">
<div>Select languages:</div>
<select name="languages" size="7" multiple="multiple">
<option value='Ada'>Ada</option>
<option value='C'>C</option>
<option value='C++'>C++</option>
<option value='Cobol'>Cobol</option>
<option value='Eiffel'>Eiffel</option>
<option value='Objective-C'>Objective-C</option>
<option value='Java'>Java</option>
</select>
<button type="submit">Submit</button>
</form>
</body>
</html>
We create a <select> control that contains seven values. When
we submit the form, the selected values are sent to the languages.jsp
file.
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="jakarta.tags.core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Languages</title>
</head>
<body>
<c:forEach items="${paramValues.languages}" var="lang">
<c:out value="${lang}"/>
</c:forEach>
</body>
</html>
The values from the <select> control are available from
the implicit paramValues object, which is a map. The key is
the request parameter name (languages) and the values are
in an array of strings.
<c:forEach items="${paramValues.languages}" var="lang">
<c:out value="${lang}"/>
</c:forEach>
We go over the array and print its elements.
$ curl 'localhost:8080/jstl/languages.jsp?languages=Java&languages=C' Java C
forEach tag example IV
The step attribute skips values in the range, and
varStatus exposes the state of the iteration. The status object
has index (zero-based), count (one-based),
first, and last properties.
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="jakarta.tags.core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Steps</title>
</head>
<body>
<c:forEach var="i" begin="1" end="10" step="3" varStatus="status">
<c:out value="${status.count}"/>. <c:out value="${i}"/><br>
</c:forEach>
</body>
</html>
The loop starts at 1, adds 3 after each iteration, and stops at 10. The
status.count value numbers the rendered rows.
$ curl localhost:8080/jstl/steps.jsp 1. 1 2. 4 3. 7 4. 10
forEach tag example V
The following example displays data in an HTML table.
<!DOCTYPE html>
<html>
<head>
<title>Start Page</title>
<meta charset="UTF-8">
</head>
<body>
<p>
<a href="MyServlet">Show all cities</a>
</p>
</body>
</html>
In the index.html page we have a link that calls MyServlet.
The servlet loads data with a service method and dispatches to the JSP page.
package com.zetcode.bean;
public class City {
private Long id;
private String name;
private int population;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public City(Long id, String name, int population) {
this.id = id;
this.name = name;
this.population = population;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
}
This is the City class; it contains id,
name, and population attributes.
package com.zetcode.web;
import com.zetcode.bean.City;
import com.zetcode.service.CityService;
import java.io.IOException;
import java.util.List;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
List<City> cities = CityService.getAllCities();
request.setAttribute("cities", cities);
request.getRequestDispatcher("showCities.jsp").forward(request, response);
}
}
The servlet reads data with CityService.getAllCities, sets the list
object to the attributes with setAttribute, and forwards to
the showCities.jsp.
package com.zetcode.service;
import com.zetcode.bean.City;
import java.util.ArrayList;
import java.util.List;
public class CityService {
public static List<City> getAllCities() {
List<City> cities = new ArrayList<>();
cities.add(new City(1L, "Bratislava", 432000));
cities.add(new City(2L, "Budapest", 1759000));
cities.add(new City(3L, "Prague", 1280000));
cities.add(new City(4L, "Warsaw", 1748000));
cities.add(new City(5L, "Los Angeles", 3971000));
cities.add(new City(6L, "New York", 8550000));
cities.add(new City(7L, "Edinburgh", 464000));
cities.add(new City(8L, "Berlin", 3671000));
return cities;
}
}
The getAllCities method returns a list of cities.
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="jakarta.tags.core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cities</title>
</head>
<body>
<h2>Cities</h2>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Population</th>
</tr>
</thead>
<tbody>
<c:forEach items="${cities}" var="city">
<tr>
<td><c:out value="${city.id}"/></td>
<td><c:out value="${city.name}"/></td>
<td><c:out value="${city.population}"/></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
In the showCities.jsp, we display the cities in the HTML table with
the <c:forEach> tag.
<td><c:out value="${city.id}"/></td>
<td><c:out value="${city.name}"/></td>
<td><c:out value="${city.population}"/></td>
The attributes are read from the city object with the dot operator.
Running the application
Run the project with the Jetty Maven plugin and open
http://localhost:8080/jstl/. The home page links to the servlet
example.
$ mvn jetty:run
The servlet forwards the city list to showCities.jsp, which
renders one table row per city.
$ curl localhost:8080/jstl/MyServlet
...
<tr>
<td>1</td>
<td>Bratislava</td>
<td>432000</td>
</tr>
...
Source
Jakarta Standard Tag Library specification and the Jakarta EE JSP tutorial.
In this tutorial, we covered the <c:forEach> tag from the
Jakarta Standard Tag Library. We iterated over a numeric range, request
parameters, a multi-value parameter, a stepped range, and a list of Java
objects.
Author
List all Java tutorials.