O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

23.3. Using the Core JSTL Tags

Problem

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

Solution

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

Discussion

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.

Tip

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" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required