JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

JSTL - Core Library

Part:   1  2  3  4 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

c:forEach Action

<c:forEach var="name" items="expression" 
   begin="expression" end="expression" step="expression">
 body
</c:forEach>

If the "items" attribute is specified, it will be used to identify an array or collection object, the elements in the array or collection will be iterated. At each iteration, the current element will assign to a named variable specified in the "var" attribute, and body will be processed. If the "begin", "end" or "step" attribute is specified, it will be used to restrict the beginning element, the ending element, or the step size of the iteration.

If the "items" attribute is not specified, an index integer will be used to iterate from the "begin" value to the "end" value stepping with the "step" value.

The c:forEach action serves similar purposes as the Java for statement. But there is no break mechanism.

c:forTokens Action

<c:forTokens var="name" items="expression" delims="expression" 
   begin="expression" end="expression" step="expression">
 body
</c:forTokens>

The string specified by the "items" attribute will be tokenized with the delimiter specified by the "delims" attribute. Then resulting tokens will be iterated. At each iteration, the current token will assign to a named variable specified in the "var" attribute, and body will be processed. If the "begin", "end" or "step" attribute is specified, it will be used to restrict the beginning element, the ending element, or the step size of the iteration.

JSTL Core Example - JstlObjects.jsp

As my first JSTL core example, JstlObjects.jsp, is to use c:forEach to browse through all the implicit objects, and c:forTekons to break the class path into multiple items.

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" 
   xmlns:c="http://java.sun.com/jstl/core" version="1.2"> 
<!-- JstlObjects.jsp
     Copyright (c) 2003 by Dr. Herong Yang
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<p>Browsing all the JSTL implicit objects:</p>
<p>"pageContext":</p>
<c:out value="${pageContext}"/><br/>

<p>"pageScope":</p>
<c:forEach items="${pageScope}" var="entry">
 <c:out value="${entry}"/><br/>
</c:forEach>

<p>"requestScope":</p>
<c:forEach items="${requestScope}" var="entry">
 <c:out value="${entry}"/><br/>
</c:forEach>

<p>"sessionScope":</p>
<c:forEach items="${sessionScope}" var="entry">
 <c:out value="${entry}"/><br/>
</c:forEach>

<p>"applicationScope":</p>
<c:forEach items="${applicationScope}" var="entry">
 <c:out value="${entry}"/><br/>
</c:forEach>

<p>"param":</p>
<c:forEach items="${param}" var="entry">
 <c:out value="${entry}"/><br/>
</c:forEach>

<p>"paramValues":</p>
<c:forEach items="${paramValues}" var="entry">
 <c:out value="${entry}"/><br/>
</c:forEach>

<p>"header":</p>
<c:forEach items="${header}" var="entry">
 <c:out value="${entry}"/><br/>
</c:forEach>

<p>"headerValues":</p>
<c:forEach items="${headerValues}" var="entry">
 <c:out value="${entry}"/><br/>
</c:forEach>

<p>"cookie":</p>
<c:forEach items="${cookie}" var="entry">
 <c:out value="${entry}"/><br/>
</c:forEach>

<p>"initParam":</p>
<c:forEach items="${initParam}" var="entry">
 <c:out value="${entry}"/><br/>
</c:forEach>

<p>Class path list:</p>
<c:forTokens
 items="${applicationScope['org.apache.catalina.jsp_classpath']}"
 delims=";" var="entry">
 <c:out value="${entry}"/><br/>
</c:forTokens>

</body></html>
</jsp:root>

(Continued on next part...)

Part:   1  2  3  4 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - JSTL - Core Library