Getting Started with the Standard Tag Library

This document describes how to get up and running quickly with 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 JSTL examples is also a great way to familiarize yourself with JSTL's functionality and use.


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, XML-manipulation tags, and useful EL functions. 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 lists books and other resources that will help you learn JSTL.

Getting started quickly

Since the Web Services Pack supports a JSP 2.0 container and makes all JSTL jar files globally available to all web applications, you are all set to immediately take advantage of JSTL 1.1 in your webapps.

Multiple tag libraries

The constituent tag libraries of Standard Taglib are as follows:

Funtional Area URI Prefix Example
<c:tagname ...>
XML processing
<x:tagname ...>
I18N capable formatting
<fmt:tagname ...>
Database access (SQL)
<sql:tagname ...>
EL Functions
fn:fnname ...

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

Complete definition of the Expression Language may be found in the JSP 2.0 specification.

Topics covered in JSTL

As we mentioned above, JSTL includes core tags to support iteration, conditionals, and expression-language support. 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.

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.
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 languages
JSTL provides a few tags to facilitate use of expression languages. <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.
EL Functions currently allow you to get the length of a collection and to manipulate strings in a variety of ways (for example, capitalization, subsetting, trimming, character replacement...)

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.