Java Development News:
JSTL In Action
By Shawn Bayern
01 Apr 2002 | TheServerSide.com
A new language for designing web pages
When I signed on with the Java Community Process to help develop the JSP Standard Tag Library (JSTL), I didn't realize it would completely change the way I thought about JavaServer Pages (JSP). JSTL serves as an add-on to JSP, but it really represents a brand-new language, one that you can use to produce dynamic web pages -- even if you've never programmed before.
For programmers and nonprogrammers alike, JSTL provides simple and reusable tools for common web-development tasks: loops, conditions, database access, XML manipulation, internationalization (i18n), text formatting, and more. In this article, we look at JSTL's basics -- its history, goals, and schedule.
What is JSTL?
JSTL is a collection of HTML-like tags that let you produce dynamic web pages without needing to learn Java. When JSTL is released in mid-2002, it will serve as a standard addition to Sun's popular JSP platform. Using JSTL with JSP, you will be able to create pages that read XML, access databases, and perform a variety of other tasks -- all without any programming in the traditional sense.
JSTL builds on a JSP feature known as custom actions or, more simply, custom tags. To understand how JSTL works, let's look at JSP and custom tags a little more closely.
Java, JSP, and custom tags
When traditional languages like Java appear within a web page, they can make the page harder to maintain. For example, JSP lets you introduce Java code right in the middle of your HTML, much like this:
<body> <p> We have now stolen <%= session.getAttribute("dollars") %> from your credit-card account. Please stop by again. </p> </body>
The code within the pseudo-tag, <%= ... %>, runs every time a browser loads the web page that contains it. The Java expression's output, which in this case would presumably be a string like "$4000", gets printed instead of the literal text between "<%=" and "%>".
At first glance, this looks convenient. But as you might know if you've managed a JSP project, the direct mixture of Java and HTML can become unwieldy. If a web designer who knows just HTML, not Java, needs to edit the file, the code within the pseudo-tag, <% ... %>, is simply magic. To modify the code, the page designer needs either to read up on Java or to call in a Java programmer for assistance. Either way, the mixed-in Java code complicates division of labor in web projects.
To address this problem, JSP supports a mechanism called custom tags. These tags look like HTML, but Java programmers can link them to Java code. For instance, a Java developer might write a method to determine a user's first name and then expose this code using a tag called <user:firstName>. Whenever this tag appears, the developer's back-end logic would run and produce the user's first name; it would then insert this name into the web page's output. So a page author could write the following:
<p> Dear <user:firstName/>, </p>
and produce dynamic content, very much like the age-old word-processing practice of mail merge.
JSTL: Standard tags
As JSP grew in popularity, it became clear that different sites' custom tags fell into familiar, frequently used patterns. For example, many pages needed to loop over data to print tables and lists. Other pages accessed information from databases, or retrieved information stored in XML files.
Vendors of JSP containers and members of the open-source community tried to address these needs by providing collections of tags -- tag libraries -- that solved common problems. For instance, JRun, the popular JSP container, began to provide a proprietary tag library of its own, and the Jakarta Taglibs project offered free libraries for tasks like XML manipulation, HTML form generation, and database access.
While many JSP page authors used these libraries, nothing tied them together. For instance, you couldn't count on your favorite tag library being available if you moved from one project to another. One site might use Jakarta Taglibs, another would depend on JRun's library, and a third could rely on custom tags produced and maintained locally. To address this fragmentation, the Java Community Process -- the same group that now maintains and standardizes JSP itself -- decided to offer a standard tag library. JSTL 1.0, the first version of this standard, will include tags for the following common tasks:
- looping over data to produce tables, lists, etc. (e.g., "print all of the user's orders")
- conditional operations (e.g., "display mature content only if the user is over 18")
- importing and processing data from other other web pages
- simple XML manipulation
- database access
- text formatting and internationalization
JSTL 1.0 is scheduled for a mid-2002 release. My upcoming book, The JSP Standard Tag Library, covers all of these topics in detail.
The flagship offering: an "expression language"
In addition to all of these specific features, JSTL was the primary motivation behind a general feature that will be introduced in the next version of JSP: an expression language. Instead of writing traditional Java/JSP expressions like
<%= session.getAttribute("username").getFirstName() %>
JSTL lets you access data using simpler expressions, such as
Not a general-purpose language, the expression language is suited to JSP in particular; its goal is to make sure JSP pages can easily access the data they need. For example, the language will let page authors access information in JSP's scopes, which are collections of data with different life-cycles. For instance, some data stays around for your whole application, but other data applies only to the current user's session and evaporates immediately once the user logs out. (See figure 1.) The expression language (whose details are still under consideration by the Java Community Process expert groups) will also most likely provide access to request parameters (for instance, data the user entered) and cookies (bits of data sent to, and returned by, web browsers).
Figure 1: JSP lets you organize data in scopes. For instance, some data applies to your entire application, but some stays around only as long as your user's session does. The new expression language will let you access data in these scopes without having to write any Java code. (Figure from chapter 2 of JSP Standard Tag Library.)
Benefits for programmers too
Web-page authors are its primary audience, but JSTL introduces a few tools for Java programmers as well. For example, if you know how to write Java code, JSTL makes it easier for you to develop your own custom tags.
To develop a custom tag, Java developers currently need to understand the somewhat detailed API and life-cycle management of tag handlers. (For more information on tag handlers, see JSP Tag Libraries, by Shachor, Chace, and Rydin.).
By contrast, JSTL provides a series of base classes to help developers produce tags. For example, programming an iteration tag with JSTL is as simple as implementing a java.util.Iterator; you don't need to know how traditional tag-handler methods like doStartTag() and doAfterBody() work. Similarly, with JSTL, producing a conditional tag is as simple as writing a boolean method. (I address these issues in more detail in part IV of JSP Standard Tag Library.)
Of course, JSTL brings another important benefit to programmers: it makes it easier for them, too, to produce web pages. JSTL's tags aren't just for nonprogrammers; even those who know how to code will appreciate JSTL's pre-made, pre-tested tags for database access, XML manipulation, and other tasks. For example, when I need to print the results of a SQL query in my JSP page, I'd much rather use JSTL's tags than manually write and test new JDBC code.
What the future holds
JSTL, along with the expression language, should be finished and released by mid-2002. Experimental versions of the reference implementation for JSTL, released under an early access program, are already available from the Jakarta Taglibs web site. My book, JSTL In Action, is planned for publication around the same time the standard arrives. Until then, Manning's web site will offer a series of articles on JSTL and other technical topics, so check back often!
To learn more about other Manning Java books, visit our Java Books page...
About the Author
Shawn Bayern is the reference-implementation lead for JSTL and the author of Manning's "JSTL In Action" which will be available July, 2002.