ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button JSP and Servlets

JSP Standard Tag Libraries, Part 1


Many of us have been working with the Java Server Pages technology for quite some time and have become familiar with custom tags. Custom tags have made working with JSPs not only easier, but also more efficient.

If a custom tag has been created, tested, and debugged, then it only is logical that, as a developer, you want to grab the golden ring of OOD: reusable components. Working with custom tags is one avenue to explore in the world of reuse. But wouldn't it be better, more efficient, and easier if there was a set of standard tags that solved common problems? Do we really need ten different ways to iterate, or to do conditional processing?

Enter the JSP Standard Tag Library, also known as JSTL. The base design philosophy of JSTL is to provide page authors with a script-free environment. I say, bottoms up to that.

JSTL is still in early access release, but that doesn't mean that you shouldn't start paying attention to it. JSTL is being developed under the Java Community Process, in the JSR-052 expert group. The purpose of JSTL is to work towards a common and standard set of custom tags. There are currently many tag libraries available for download from various vendors, as well as from open source projects such as the Jakarta Taglibs project. In fact, a JSP 1.2/Servlet 2.3-compliant reference implementation of JSTL is being hosted under the Taglibs project. You can run this reference implementation under Tomcat, but you'll need to use Tomcat 4.

Remember, this is still an early-access release, so there's always the chance that things will change slightly. The tags provided in JSTL will be able to be used within any JSP-compliant container. The advantage of using JSTL over a vendor-specific tag library is, obviously, that you won't be bound to a specific vendor's container.

Ready for a look at the wizard behind the curtain?

Various Tag Libraries

A tag library is a set of actions that encapsulate functionality. These tags are then used within JSP pages. JSTL provides a wide variety of functionality that can be broken down into specific functional areas. While JSTL is a single taglib, it is exposed through multiple Tag Library Descriptors (TLDs). This is done primarily for convenience (so that tags are in their appropriate functional area), but also so that each TLD can have its own namespace, or prefix. These four areas are:

  • Core (with a URI of http://java.sun.com/jstl/ea/core and a prefix of c): provides the core JSTL tags, such as those that perform iteration, conditional processing, and expression language support. Access tags provided in this TLD by placing < %@ taglib prefix="c" uri="http://java.sun.com/jstl/ea/core" %> at the top of your JSP.

  • XML processing (with a URI of http://java.sun.com/jstl/ea/xml and a prefix of x): provides tags that allow parsing and XSL transformation of XML documents.

  • I18N-capable formatting (with a URI of http://java.sun.com/jstl/ea/fmt and a prefix of fmt): provides tags that support I18N and localized formatting and parsing.

  • Database access (SQL) (with a URI of http://java.sun.com/jstl/ea/sql and a prefix of sql: provides tags that allow direct database access from within JSPs.

One other point to mention about the JSTL tag libraries is that there are actually two versions each of the majority of the tags. One set takes advantage of the new expression language, and the other doesn't; the tags that don't make use of an expression language use request-time expressions. This is useful if you want the flexibility provided by the standard tags but don't want to use the expression language support yet. To access these tag libraries, simply append -rt to the end of the taglib URI like so: <%@ taglib prefix="c" uri="http://java.sun.com/jstl/ea/core-rt" %>

Tag, You're It

Let's take an overview look at the various tags in the functional areas outlined above.

Related Reading

Java Servlet Programming
By Jason Hunter

One of core tags is <forEach>, which provides iteration functionality: it iterates over most collections. It can be used with ranges or primitives, and it also can provide a detailed status of the iteration. <forTokens> works like <forEach>, except that it is applied to strings of tokens and takes an extra attribute called delims that allows for the delimiter to be specified. You can have more than one delimiter specified.

Also in the core functional area are the conditional tags. JSTL supports a simple conditional <if> tag along with a collection of tags such as <choose>, <when>, and <otherwise>. These tags support mutually exclusive conditionals. By using these tags, you can implement if/else structures. In its API, JSTL also exposes the abstract class ConditionalTagSupport, to facilitate the implementation of custom conditional tags that leverage the standard conditional behavior defined in JSTL.

Expression language (EL) support is an important feature of JSTL that we'll talk about in more detail in the next section. It is also part of the core functionality. JSTL provides a few tags to facilitate the use of expression language. <c:expr> 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 (usually Java). <c: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. There is also the <c:declare> tag. In order for JSTL tags to collaborate with custom tags that only accept rtexprvalues, the <c:declare> tag must be used to create a scripting variable.

While JSP supports the jsp:include tag, this standard action is limited because it only supports relative URLs. JSTL introduces the c:import tag, which lets you retrieve absolute URLs. You can use c:import to retrieve information from the Web using HTTP URLs, or from a file server using an FTP URL. You are even able to specify a foreign context, and that can come in handy sometimes.

Formatting data is one of the key tasks in many JSP pages. JSTL introduces tags to support data formatting and parsing for such things as dates and numbers. Using <fmt:locale>, <fmt:bundle>, and <fmt:message>, you can easily specify locale information for determining which resource bundles to use. These tags can also be used to support parameterized content using the <fmt:messageArg> tag. It's possible to use these tags very easily in your internationalized applications.

Pages: 1, 2

Next Pagearrow