Getting Started with the Standard Tag Library

This document describes how to get up and running quickly with the Standard Taglib, an implementation of the Java Server Pages™ Standard Tag Library (JSTL). This document may be useful to page authors and tag developers who are interested in JSTL's functionality. Using the "standard-examples" application is also a great way to familiarize yourself with JSTL's functionality and use.


Introduction

What is JSTL? Where does it come from?

JSTL is the Java Server Pages Standard Tag Library. It is an effort of the Java Community Process (JCP) and comes out of the JSR-052 expert group.

What does JSTL do?

JSTL encapsulates, as simple tags, core functionality common to many JSP applications. For example, instead of suggesting that you iterate over lists using a scriptlet or different iteration tags from numerous vendors, JSTL defines a standard <forEach> tag that works the same everywhere.

This standardization lets you learn a single tag and use it on multiple JSP containers. Also, when tags are standard, containers can recognize them and optimize their implementations.

JSTL provides support for core iteration and control-flow features, text inclusion, internationalizaton-capable formatting tags, and XML-manipulation tags. The expression language that JSTL defined in the 1.0 version of the specification is now an integral part of the JSP 2.0 specification. Developers may also be interested in JSTL's current extensibility mechanisms; JSTL currently provides a framework for integrating custom tags with JSTL tags.

What has changed in this Standard taglib release?

Please see the Release Notes document for information on any release changes.

How can I learn more about JSTL

Sun's official JSTL page at http://java.sun.com/products/jstl lists books and other resources that will help you learn JSTL.


Getting started quickly

JSTL 1.1.1 requires a JSP 2.0 container. The following are acceptable JSP 2.0 containers:

You can dowload these containers from here:
http://java.sun.com/webservices/containers/index.html.

When you install the Java WSDP, the installer will detect the compliant containers available on your system  and ask you which container you would like to use.

If you are using the Application Server as your container, keep in mind that the JSTL JAR files are installed but not integrated into the container.  This is because the Application Server already comes with the same JSTL JAR files.

The Java WSDP includes a set of example applications that use JSTL.  To run the JSTL examples on the Application Server, do the following:

To run the JSTL examples while using the Tomcat container, launch Tomcat and open the following URL in your browser: http://localhost:8080/jstl-examples.  The Examples section of this page has a list of examples you can run.

To use JSTL in your own application, simply copy the JAR files in the distribution's jstl/lib directory to your application's WEB-INF/lib directory. The following table lists the JAR files that you need to copy

Name Description Jar File Name
JSTL API classes
JSTL API classes
jstl.jar
JSTL implementation classes
Standard Taglib JSTL implementation classes
standard.jar

The standard tag library also has the following dependencies:

However, since all of these dependencies are included in J2SE 1.4.2 and higher, it is therefore recommended to use J2SE 1.4.2 or higher to avoid having to worry about these other dependencies.

If the java platform under which you run your JSP container does not provide these dependencies, they must be made available either globally to all web-applications by your container, or individually within the WEB-INF/lib directory of your web-application.

Multiple tag libraries

The constituent tag libraries of Standard Taglib are as follows:

Funtional Area URI Prefix Example
Core http://java.sun.com/jsp/jstl/core
c
<c:tagname ...>
XML processing http://java.sun.com/jsp/jstl/xml
x
<x:tagname ...>
I18N capable formatting http://java.sun.com/jsp/jstl/fmt
fmt
<fmt:tagname ...>
Database access (SQL) http://java.sun.com/jsp/jstl/sql
sql
<sql:tagname ...>
Functions http://java.sun.com/jsp/jstl/functions
fn
fn:functionName(...)

Using the Standard Taglib libraries is simple; you simply need to import them into your JSP pages using the taglib directive. For instance, to import the 'core' JSTL library into your page, you would include the following line at the top of your JSP page, as follows:

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

Expression language

The EL makes it easy for page authors to access and manipulate application data. For an overview of the EL, see Chapter 3 of the JSTL Specification.

Topics covered in JSTL

As we mentioned above, JSTL includes core tags to support iteration, conditionals, and expression-language support. It also supports EL functions for string manipulation. For more information on precisely how these tags work, you should read the JSTL specification. Here, we just offer a quick roadmap of each feature in order to help orient you.

Iteration
The core iteration tag is <forEach>, which iterates over most collections and similar objects you'd think to iterate over. <forTokens> lets you iterate over tokens in a String object; it lets you specify the String and the delimiters.
Conditionals
JSTL supports a simple conditional <if> tag along with a collection of tags -- <choose>, <when>, and <otherwise> -- that support mutually exclusive conditionals. These latter three tags let you implement a typical if/else if/else if/else structure.
Expression language
JSTL provides a few tags to facilitate use of the expression language. <out> prints out the value of a particular expression in the current EL, similar to the way that the scriptlet expression (<%= ... %>) syntax prints out the value of a expression in the scripting language (typically Java). <set> lets you set a scoped attribute (e.g., a value in the request, page, session, or application scopes) with the value of an expression.
Text inclusion
JSP supports the jsp:include tag, but this standard action is limited in that it only supports relative URLs. JSTL introduces the c:import tag, which lets you retrieve absolute URLs. For instance, you can use c:import to retrieve information from the web using HTTP URLs, or from a file server using an FTP URL. The tag also has some advanced support for performance optimizations, avoiding unnecessary buffering of data that's retrieved.
I18N-capable text formatting
Formatting data is one of the key tasks in many JSP pages. JSTL introduces tags to support data formatting and parsing. These tags rely on convenient machinery to support internationalized applications.
XML manipulation
You can't look anywhere these days without seeing XML, and JSTL gives you convenient support for manipulating it from your JSP pages. Parse documents, use XPath to select content, and perform XSLT transformations from within your JSP pages.
Database access
Easily access relational databases using the SQL actions. You can perform database queries, easily access results, perform updates, and group several operations into a transaction.
Functions
String manipulations can be performed using the functions provided in JSTL.
 

For tag developers...

Developers of custom tags should also read the JSTL specification. JSTL defines some abstract classes that assist with rapid development of tags and promote integration of custom tags with JSTL's tag set.

For instance, extending javax.servlet.jsp.jstl.core.ConditionalTagSupport lets you write a conditional tag by merely implementing a single method that returns a boolean value correspondent with your tag's desired conditional behavior; also, this base class promotes JSTL's recommended model of conditional-tag design.

Similarly, javax.servlet.jsp.jstl.core.IteratorTagSupport lets you easily implement iteration tags. The handlers for the <forEach> and <forTokens> tags extend this class and thus implement the javax.servlet.jsp.jstl.core.IteratorTag interface, which provides a well-defined mechanism for iteration tags to communicate with custom subtags you can write. See the "standard-examples" application for one example of how you might use such custom subtags.