JSTL Core c:forEach

It works like looping statement to iterate over collection or fixed number of times.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach var="number" begin="5" end="10">  <c:out value="${number}"></c:out>

Example below to work with collection.Here collection is set in servlet or controller and then iterate through c:forEach in JSP.

FirstServlet .java

import javax.servlet.http.*; 
import javax.servlet.*; 
import java.io.*; 
public class FirstServlet extends HttpServlet{ 
public void doGet(HttpServletRequest request,HttpServletResponse response) 
throws ServletException,IOException { 
ArrayList<String> list = new ArrayList<String>(); 
 list.add("Item 1"); 
 list.add("Item 2");
 list.add("Item 3");
 RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/jsp/list.jsp");


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach items="list" var="data"> 
<c:out value="${data}"></c:out>

Leave a Reply