Deconstructing <c:forEach>

The <c:forEach> tag maps nicely into a for loop—the tag repeats the body of the tag for each element in the collection (and we use “collection” here to mean either an array or Collection or Map or comma-delimited String).

The key feature is that the tag assigns each element in the collection to the variable you declare with the var attribute.

image with no caption

Getting a loop counter with the optional varStatus attribute

image with no caption
image with no caption

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.