How to apply forEach tag in JSP?


The <c:forEach> tag is a commonly used tag because it iterates over a collection of objects. The <c:forTokens> tag is used to break a string into tokens and iterate through each of the tokens.

Attribute

The <c:forEach> tag has the following attributes −

AttributeDescriptionRequiredDefault
itemsInformation to loop overNoNone
beginElement to start with (0 = first item, 1 = second item, ...)No0
endElement to end with (0 = first item, 1 = second item, ...)NoLast element
stepProcess every step itemsNo1
varName of the variable to expose the current itemNoNone
varStatusName of the variable to expose the loop statusNoNone

Example for <c:forEach>

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<html>
   <head>
      <title><c:forEach> Tag Example</title>
   </head>
   <body>
      <c:forEach var = "i" begin = "1" end = "5">
         Item <c:out value = "${i}"/><p>
      </c:forEach>
   </body>
</html>

The above code will generate the following result −

Item 1
Item 2
Item 3
Item 4
Item 5

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

526 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements