[ Team LiB ] Previous Section Next Section

Recipe 23.1 Downloading the JSTL 1.0 and Using the JSTL Tags in JSPs

Problem

You want to download and use the JSTL.

Solution

Download the JSTL distribution, in a ZIP or TAR file, from the Apache Jakarta Project.

Discussion

The Apache Jakarta Project hosts the reference implementation (RI) for the JSTL. An RI is software that is designed to implement a particular Java technology specification in order to demonstrate how the software is intended to function. RIs are freely available for use by software vendors and developers. You can download the binary or source distribution of the JSTL from http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html.

Unpack the ZIP or TAR file into the directory of your choice. This creates a jakarta-taglibs directory.

This recipe uses the Standard Taglib Version 1.0.3, an implementation of the JSTL 1.0. However, by the time you read this, the Jakarta Taglibs site will have introduced Standard Taglib Version 1.1, which is an implementation of the JSTL 1.1. The new version includes some new features such as functions, which are described in Recipe 23.14.


Inside the standard-1.0.3 directory is a lib subdirectory. This directory contains a number of JAR files, including jstl.jar and standard.jar. The jstl.jar contains the JSTL 1.0 API classes; standard.jar is a collection of JSTL 1.0 implementation classes. Add all of the JAR files found in your JSTL distribution's lib directory (jakarta-taglibs/standard-1.0.3/lib in the example) to WEB-INF/lib.

JSTL 1.1 only requires the installation of jstl.jar and standard.jar in /WEB-INF/lib if you are using J2SE 1.4.2 or higher (as well as Servlet 2.4 and JSP 2.0).


Table 23-1 describes each of the JAR files found in the distribution's lib directory (courtesy of the Standard Taglib 1.0 documentation).

Table 23-1. Contents of the JSTL 1.0 reference implementation lib directory

File name

Purpose

jstl.jar

JSTL1.0 API classes

standard .jar

JSTL1.0 implementation classes

jaxen_full.jar

Xpath engine classes

jdbc2_0-stdext.jar

Java Database Connectivity (JDBC) implementation classes (also included with J2SE 1.4)

saxpath.jar

Simple API for Xpath parsing

xalan.jar

Apache Xalan Extensible StyleSheet Transformations (XSLT) processor

dom.jar, jaxp-api.jar, sax.jar, xercesImpl.jar

Java API for XML Processing (JAXP) 1.2 API libraries

In the JSP where you want to use the JSTL tags, use the proper taglib directive shown in Table 23-2. For example, if you use all of the different JSTL functions (core, XML, formatting, and SQL), your JSP contains all of the following taglib directives, preferably at the top of the JSP page (they must appear before the tags are used).

Table 23-2. The taglib directives for different JSTL functions, version 1.0

JSTL library

taglib directive

Core

<%@ taglib uri="java.sun.com/jstl/core" prefix="c" %>

XML processing

<%@ taglib uri="java.sun.com/jstl/xml" prefix="x" %>

Formatting data (such as dates and currencies) for international users

<%@ taglib uri="java.sun.com/jstl/fmt" prefix="fmt" %>

SQL and Database access

<%@ taglib uri="java.sun.com/jstl/sql" prefix="sql" %>

The Java community is now working on the JSTL Version 1.1, which will require a JSP-2.0 compatible JSP container. JSTL 1.1 will use these different uri values in the taglib directive:

  • http://java.sun.com/jsp/jstl/core, so the entire taglib directive would look like: <%@ taglib uri="java.sun.com/jsp/jstl/core" prefix="c" %>

  • http://java.sun.com/jsp/jstl/xml, creating a taglib directive of: <%@ taglib uri="java.sun.com/jsp/jstl/xml" prefix="x" %>

  • http://java.sun.com/jsp/jstl/fmt, as used in the taglib directive: <%@ taglib uri="java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

  • http://java.sun.com/jsp/jstl/sql, creating a taglib directive of: <%@ taglib uri="java.sun.com/jsp/jstl/sql" prefix="sql" %>

See Also

The Jakarta Project's Taglibs site: http://jakarta.apache.org/taglibs/index.html; Sun Microsystem's JSTL information page: http://java.sun.com/products/jsp/jstl/; Recipe 23.3 on using the core tags; 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