[ Team LiB ] Previous Section Next Section

Recipe 23.3 Using the Core JSTL Tags


You want to use the core JSTL tags in a JSP.


Use the taglib directive with the core uri attribute value to make the tags available in the JSP.


This recipe demonstrates several JSTL tags that you use all the time: c:set, c:out, c:forEach, and c:if. Here are the tag summaries:

  • The c:set tag sets object attributes to page, request, session, or application scopes.

  • The c:out tag displays text literals or the values of variables or bean properties in your JSPs.

  • The c:forEach tag iterates over Maps, Collections, and arrays.

  • The c:if tag tests expressions for true or false values, then conditionally executes the code nested in the c:if body.

Remember to use the prefix for the certain functional area of the JSTL, such as c, followed by a colon, and the tag name, as in "c:forEach."

Example 23-1 is a helper class that I find necessary to properly return a String array of TimeZone IDs to the JSP in Example 23-2.

Example 23-1. A helper class to help display TimeZone IDs
package com.jspservletcookbook;           

import java.util.TimeZone;

public class ZoneWrapper  {

    public ZoneWrapper( ){}
        public String[] getAvailableIDs( ){
            return TimeZone.getAvailableIDs( );


Example 23-2 shows how to use a number of the core JSTL tags. The code uses the jsp:useBean standard action to create ZoneWrapper (Example 23-1) and java.util.Date objects for use by the tags.

Example 23-2. Using core JSTL 1.0 tags in a JSP
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<jsp:useBean id="zone" class="com.jspservletcookbook.ZoneWrapper" /> 

<jsp:useBean id="date" class="java.util.Date" />

<head><title>Using the Core JSTL tags</title></head>
<h2>Here are the available Time Zone IDs on your system</h2>

<c:if test="${date.time != 0}" >

    <c:out value=
      "Phew, time has not stopped yet...<br /><br />" escapeXml="false"/>


<%-- The variable 'zones' contains a String array of TimeZone IDs; it is stored as a 
'session' object attribute. The '${zone.availableIDs}' expression is the equivalent of 
calling the ZoneWrapper.getAvailableIDs( ) method --%>

<c:set var="zones" value="${zone.availableIDs}" scope="session" />

<c:forEach var="id" items="${zones}">

        <c:out value="${id}<br />" escapeXml="false" />



The c:if tag uses an EL phrase to test whether the Date object's getTime( ) method returns a value that is not zero (of course it does! I'm just demonstrating how to use the c:if tag).

${date.time != 0}

The prior code represents a boolean expression that returns true if Date.getTime( ) is greater than zero. If true, then the code executes the nested c:out tag, which writes a message that the client's browser displays.

The escapeXml="false" code displays the characters <br /><br /> correctly in the HTML output by the c:out tag. See Table 23-3.

Example 23-2 sets an object attribute to session scope. This object is a String[] type containing time zone IDs, such as "Pacific/Tahiti." The c:forEach tag then iterates over all of these array members, displaying each ID with the c:out tag:

<c:forEach var="id" items="${zones}">
    <c:out value="${id}<br />" escapeXml="false" />

The var attribute of the c:forEach tag stores the current array member as c:forEach cycles over the collection. The c:out tag uses an EL expression to access the value of the current array member:

<c:out value="${id}<br />" escapeXml="false" />

If you do not give the escapeXml attribute a false value when using c:out , the character entity codes shown in Table 23-3 will display instead of the escaped characters.

Table 23-3. The c:out tag's escaped characters

c:out value attribute character

Character entity code











Figure 23-1 shows a part of the JSP using the code in Example 23-2.

Figure 23-1. A JSP using the various core tags to display time zone IDs

See Also

Recipe 6.8 on including content in a JSP with the c:url tag; the Jakarta Project's Taglibs site: http://jakarta.apache.org/taglibs/index.html; the Sun Microsystems JSTL information page: http://java.sun.com/products/jsp/jstl/; Recipe 23.4 and Recipe 23.5 on using XML-related tags; Recipe 23.6 on using the formatting tags; Recipe 23.7 and Recipe 23.8 on the JSTL's SQL features; Recipe 23.9-Recipe 23.14 on using the EL to access scoped variables, cookies, and JavaBean properties.

    [ Team LiB ] Previous Section Next Section