JSTL forEach Tag Example



This example helps you understand and use the <c:forEach> tag in the JSTL core tags library.
All JSP Examples at JSP Source Code Examples.
<c:forEach> tag in JSTL is used for executing the same set of statements for a finite number of times. It’s similar to the for loop in java. This is a basic iteration tag, accepting many different collection types and supporting subsetting and other functionality.

Syntax of <c:forEach>

<c:forEach var="counter_variable_name" begin="intial_value" end="final_limit">
    //Block of statements
</c:forEach>
The below are the three main attributes of <c:forEach> tag.
  • begin: The initial counter value.
  • end: The final limit till which the loop will execute
  • var: Counter variable name

JSTL <c:forEach> Tag Example

In this example, we will iterator over a list of students using JSTL <c:forEach> tag.
Let's first create Student bean class:
package net.javaguides.jstl;

public class Student {

    private String firstName;
    private String lastName;
    private boolean goldCustomer;

    public Student(String firstName, String lastName, boolean goldCustomer) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.goldCustomer = goldCustomer;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public boolean isGoldCustomer() {
        return goldCustomer;
    }

    public void setGoldCustomer(boolean goldCustomer) {
        this.goldCustomer = goldCustomer;
    }
}
Let's create "for-each-student-test.jsp" JSP page and write code to iterator over a collection of students.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

    <%@ page import="java.util.*,net.javaguides.jstl.Student"%>

        <%
 // just create some sample data ... normally provided by MVC
 List<Student> data = new ArrayList<>();

 data.add(new Student("Ramesh", "Fadatare", false));
 data.add(new Student("John", "Cena", false));
 data.add(new Student("Tom", "Cruise", false));
 data.add(new Student("Tony", "Stark", false));
 data.add(new Student("Prakash", "Jadhav", true));
 pageContext.setAttribute("myStudents", data);
%>
            <html>

            <body>

                <h1>List of students</h1>
                <table border="1">

                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Gold Customer</th>
                    </tr>

                    <c:forEach var="tempStudent" items="${myStudents}">

                        <tr>
                            <td>${tempStudent.firstName}</td>
                            <td>${tempStudent.lastName}</td>
                            <td>${tempStudent.goldCustomer}</td>
                        </tr>

                    </c:forEach>

                </table>

            </body>

            </html>
Let's invoke this JSP page from a Web browser, you see the table on a browser:
All JSP Examples at JSP Source Code Examples

Comments