JSTL forEach List Iteration

JSTL forEach List Iteration Example explains about How to Iterating a List using JSTL Taglib

List is an ordered pair of objects, It may contains duplicates, but it keeps correct the insertion order

Consider a JSP page, which contains a ArrayList with lot of items inside it and we need to iterate and display the items inside that particular ArrayList

You can access an element from this collection using ${item} using a variable var="item"

This JSTL 'c:forEach is available for all the List / Collection implementations in java

Following example shows How to iterate a List using JSTL c:forEach

Note

Please check the table, You can iterate following table items using c:forEach tag

JSTL foreach Collections

Note

You can see Map Iteration here JSTL Map Iteration Example

Required Libraries

You need to download

  1. Tomcat 9
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

Project Structure

JSTL forEach List Iteration

JSTL List Example

// JSTL c:forEach Example
    

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%
    ArrayList<String> numList = new ArrayList<String>();
    numList.add("1");
    numList.add("2");
    numList.add("3");
    request.setAttribute("numList", numList);
%>
<html>
<body>
    <c:forEach items="${numList}" var="item">
        ${item}
    </c:forEach>
</body>
</html>
Output
1 2 3










Your email address will not be published. Required fields are marked *