Using JSTL



Using JSTL

JSTL includes a wide variety of tags that naturally fit into discrete functional areas. Therefore, JSTL is exposed via multiple TLDs to clearly show the functional areas it covers and give each area its own namespace. Figure summarizes these functional areas along with the logical TLD names and prefixes used in this chapter and Duke's Bookstore application.

To use the JSTL core tags in a JSP page, you declare the library using a taglib directive that references the TLD:


<%@ taglib uri="/jstl-core" prefix="c" %>

The JSTL tag libraries comes in two versions (see Twin Libraries (page 470)). The TLDs for the JSTL-EL library are named prefix .tld. The TLDs for the JSTL-RT library are named prefix -rt.tld. Since this chapter and examples use logical TLD names, we map the names to actual TLD locations with a taglibelement in web.xml:


<taglib> 
    <taglib-uri>/jstl-c</taglib-uri> 
    <taglib-location>/WEB-INF/c.tld</taglib-location> 
</taglib>

In the Java WSDP, the JSTL TLDs are stored in <JWSDP_HOME >/tools/jstl. When you build the Duke's Bookstore application these TLDs are automatically copied into docs/tutorial/examples/web/bookstore4/build/ WEB-INF. You can also reference a TLD in a taglib directive with an absolute URI:

When you use an absolute URI, you do not have to add the taglib element to web.xml; the JSP container automatically locates the TLD inside the JSTL library implementation.

JSTL Tags

Area

Function

Tags

TLD

Prefix

Core

Expression Language

<expr>

/jstl-c

c

Support

<set>

Flow Control

<forEach>

<forEachToken>

<if>

<choose>

<when>

<otherwise>

Import

<import>

<param>

<urlEncode>

XML

Core

<parse>

/jstl-x

x

<expr>

<set>

Flow Control

<forEach>

<if>

<choose>

<when>

<otherwise>

Transformation

<transform>

<param>

<transformer>

I18n

Locale

<locale>

/jstl-fmt

fmt

Message formatting

<bundle>

<message>

<messageFormat>

<messageArg>

Number and date formatting

<formatNumber>

<parseNumber>

<timeZone>

<formatDate>

<parseDate>

Database

 

<driver>

/jstl-sql

sql

SQL

<transaction>

<query>

<update>

<param>

In addition to declaring the tag library, you also need to make the JSTL API and implementation available to the Web application. In the Java WSDP, these JSTL libraries are respectively jstl.jar and standard.jar, and are stored in <JWSDP_HOME >/tools/jstl. When you build the Duke's Bookstore application, these libraries are automatically copied into docs/tutorial/examples/web/bookstore4/build/WEB-INF/lib.