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


AddThis Social Bookmark Button

JSP Standard Tag Libraries, Part 1
Pages: 1, 2

Using the XML processing tags provided in JSTL, you can manipulate XML from your JSP pages. This includes document parsing using <x:parse> and using the <x:expr> tag to specify XPath to select content, as well as performing XSLT transformations from within your JSP pages by using the <x:transform> tag. You can also use <x:foreach> to iterate over XML nodes.

You can easily access relational databases using SQL actions. By using various tags available (including <sql:driver>, <sql:transaction>, <sql:update>, and <sql:query>), you can do transaction-based database access. It is questionable, as a Java architect, why you would want to provide tags that mix presentation and data tier access. In theory, projects should be based on clean MVC architectures; however, page authors often face a different reality out there in the real world, and the need simply cannot be ignored. It is not encouraged to access the data tier in JSP pages, but if a page author must go that route (for whatever reason), doing so with a set of standard tags seems to be the lesser evil. I strongly feel that these SQL tags should only be used in prototyping or very simple applications, if at all. You really should be separating your presentation and logic tiers, especially a data tier.

Express Yourself

One of the key features of JSTL is that it supports an expression language. An expression language leverages the fact that JSP-scoped attributes are the privileged way to communicate information from the business logic to the JSP pages. Why is this important? Well, think about how we have to do this now. If you are using JavaBeans or their properties with a custom tag, you must either have request-time expressions or make use of other functionality, such as the powerful tags that are provided with frameworks like Jakarta Struts. By using expression language in the tag, it's possible to provide values for an attribute of a tag and manipulate it in simple ways, without having to use scriptlets or request-time expression values. This sounds like something that might (or should) just be in the JSP specification, and maybe at some point it will be. But for now, it's part of the JSTL.

In order to be able to support both the scripting (rtexprvalues) and the expression language (elexprvalues) worlds, JSTL has the concept of twin tag libraries: one that supports the expression language, and one that supports request-time expression values. It's assumed that most developers will use the EL-based tag libraries; however, it's still possible for scripting page authors to use JSTL with rtexprvalues. Using the rtexprvalues will provide the benefit of type safety and performance via the request-time based tag libraries (their URI simply has the -rt suffix appended).

The early access download contains several candidate expression languages and the expert group is looking for feedback. The goal is to select a single EL once the JSTL spec is ready for community review. Currently, ECMAScript is the default expression language being used by JSTL. ECMAScript is the standardized version of JavaScript, and behaves the same way in all applications that support the standard. Rhino 1.5, an open source Java implementation of JavaScript, is one of the supported JSTL ELs.

For example, if we assume that we have an object bound in our page called cd, we can use a expression to get the value of the artist property.

  <c:if test="$cd.artist == 'Madonna'">

Notice that the expression starts with a $ symbol. This is because something is needed to indicate that the string should be evaluated (and is not a static string). Most of the expression languages available in JSTL make use of a special symbol (usually the $ character) to indicate this.

It's important to note that the whole idea of expression languages is being carefully watched and evaluated by the expert group. Because of this, you should think of it as more experimental than the rest of the JSTL. The community feedback will most likely determine whether it stays in the final version of the JSTL.

Extending JSTL

It is also possible that you can use JSTL as a base for developing your own custom tags. Some abstract classes are provided that can assist with rapid development of tags and promote integration of your own custom tags with JSTLs tag set.

For instance, you can build your own custom tags that make use of the JSTL's EL mechanism. By extending javax.servlet.jsp.jstl.core.ConditionalTagSupport, you can write a conditional tag by simply implementing a single method that returns a boolean value corresponding with your tag's desired conditional behavior. 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 may write.

There are a number of samples provided with the standard-examples application that might prove helpful. Not only are samples of the tags provided, but there are also samples that induce errors, so that you can get familiar with what some of the errors mean. This application is part of the early access download. You can just drop the standards-example.war file into the webapps directory of Tomcat and access it in your browser at

What to Watch For

With the early access release of JSTL and the reference implementation provided on the Jakarta site, there is good reason to take a few minutes and explore what's going to be available. It remains to be seen how the expression language support will play out in the community. While some might welcome it, I can see how other people might feel that it is just introducing another way to have code still involved on your JSPs. The spec was scheduled to go into public review on March 12, 2002. The more developers who have a chance to play with the JSTL before the spec is final, the better it will suit the development community. So download JSTL, give it a spin, and go have yourself a good old custom tag time.

In the next article in this JSTL series, we'll look at the practical use of some of the tags. Stay tuned.

Sue Spielman is an associate editor for ONJava.com, covering JSP and Servlets technologies. She is also President and Senior Consulting Engineer for Switchback Software LLC.

Read more JSP and Servlets columns.

Return to ONJava.com.