< Zurück | Inhalt | Weiter >

19.3.7 Tag Libraries

Well, we’re almost done with JSP, but the one topic that we have yet to cover is huge. It’s the trap door, or the way back out, through which JSP can get to lots of other code without the JSP author having to write it. Tag libraries are specially packaged libraries of Java code that can be invoked from within the JSP. Just like the useBean, they can do a lot of work behind the scenes and just return the results.

There are lots of available libraries, which is one reason for this topic to be so huge. We could spend chapters just describing all the various database access routines, HTML generating routines, and so on available to you. Perhaps the leading tag library is the JSP Standard Tag Library (JSTL).

Here are two of the most common directives used with tag libraries. First is the directive that declares a library to be used:

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

You then use the prefix as part of the tag name on subsequent tags that refer to this library. For example, if we had an out directive in our library, we could use my as the prefix, separated by a colon: <my:out ...>.

The second directive we will show is a for loop. The for loop mechanism provided by this library is in some ways simpler than using Java scriptlets. It comes in many forms, including one for explicit numeric values:

<my:forEach var="i" begin="0" end="10" step="2">

This example will loop six times with i taking on the values 0, then 2, then 4, and so on. Another variation of the forEach loop can also make it easy to set up the looping values:

<my:forEach var="stripe" items="red,white,blue">

In this example it will parse the items string into three values: red, white, and blue, assigning each, in turn, to the variable stripe. In fact the items attribute can also store an array, or collection, or iterator from the Java code that you may have declared (or that is implicit from the underlying

servlet). The forEach will iterate over those values without you having to code the explicit next() calls or index your way through an array.

The bottom of the loop is delimited by the closing tag:


For more information on these and other tags, check out



• The references at the end of this chapter

Beyond the standard library of tags, there are other third-party collections of tags; you can also create your own libraries, called custom tag libraries. While a useful and powerful thing to do if you have a large JSP-based application, such details would expand this book well beyond its scope. If you’re interested in this topic, please follow up with some of the excellent references at the end of this chapter.


We could have taken the BudgetPro example from the previous chapter and simply translated it all into JSP files. The reason we didn’t is that it’s not how you are likely to find JSP used “in the wild.” Since JSP files become servlets, it is not uncommon to find JSP and servlets mixed together—not arbitrarily, but in a sensible way. Remember the Model/View/Controller (MVC) pattern from your readings on object-oriented programming and design patterns?2 Well, JSP makes for a reasonable View, and a plain servlet can act as the Controller. The Model is typically the database behind all this. That’s what we’ve done with the BudgetPro example.

We’ve taken the two main chunks of output code—that for the main ac- count display and the form used for creating subaccounts—and turned those


2. If not, then a) you should do some more reading, and b) the MVC pattern is a “classic” way to divide the work of a GUI into three distinct parts: Model—the data behind what you are doing or displaying; View—a particular way to display that data; and Controller—an object that acts as the “traffic cop” to various inputs and events, sending messages to either the View, or Model, or both.

19.5 Review 439


into JSP files. The main servlet class (BudgetProServlet.java) is thus “gut- ted” of its output, and the new version (BudgetProController.java) acts as the controller. Requests come to it via HTTP requests, but for output, it redirects the browser making that request over to the appropriate JSP.

This introduces a new bit of servlet syntax—redirecting a request to another URL. The action is taken by means of a method call on the HTTP response object:


Whereas in the previous, servlet version of BudgetPro, we would create an object that was the next page of output:

nextPage = new AccountView(current);

In this version, we instead redirect the response to a JSP that produces the output for that page.

So how does the JSP know for which account it should display informa- tion? That is shared between the JSP and the controller servlet via the session information. As with the previous, servlet-base BudgetPro, the session is used to store the current account. It can be retrieved from the session information, as seen in line 11 of BPAcct.jsp:

11: <% Account acct = (Account) session.getAttribute("current");

That variable (acct) is then used throughout the JSP to get the appropri- ate data for display, as in:

21: Account: <%= acct.getName() %>

We could also have used a session JavaBean. Such a mechanism requires more setup on both sides, the controller and the JSP, but has the advantage of removing more literal Java code from the JSP. (“We leave this as an exercise for the reader!”)


We’ve looked at server-side Java processing with JavaServer Pages which can be thought of as servlets turned inside out. From that simple concept we looked

at our servlet example and converted it to use JSP. We also looked briefly at the syntax for JSP tags and the JSTL, but encouraged you to do more reading on this topic.


We didn’t yet discuss the spelling of JavaServer Pages. If you’ve read through this chapter, you may have noticed that there is no space between Java and Server but there is a space between Server and Pages. If you’ve read this chapter, you may also have some idea of why it’s spelled this way: It’s the JavaServer that’s doing the work—serving up the Pages. OK, it’s not a huge deal, but it is worth knowing how to spell something kerectly, rite?

There are volumes that we could have written about tag libraries. Large scale projects, and any project with a database connection behind it, will find tag libraries invaluable at providing standard mechanisms for database access. Check out the resources, below, for more information on tag libraries.


Some of the best material on JavaServer Pages comes from two of the books we mentioned in the previous chapter. You now understand how interrelated the two topics of servlets and JSP are, and these two books cover both topics very well:

Core Servlets and JavaServer Pages by Marty Hall and Larry Brown, ISBN 0-13-009229-0, a Prentice Hall PTR book.

• Its sequel, More Servlets and JavaServer Pages by Marty Hall, ISBN 0-13-067614-1, also by Prentice Hall PTR.

As we said, the topic of tag libraries is huge, and just writing about JSTL could fill it’s own volume. It has. We recommend:

Core JSTL: Mastering the JSP Standard Tag Library by David Geary, ISBN 0-13-100153-1, Sun Microsystems Press.


19.8 Exercises 441

To get it straight from the horse’s mouth, there is the official Sun specifications for JSP, available at

http://java.sun.com/products/jsp/ download.php#specs


1. Convert the controller and the JSP to share their data via JavaBeans.

2. Add a control button to each page (BPAcct.jsp) to return not just one level upwards, but back to the top level account. (Hint: The controller can store a reference to the top level account in a session variable named top.)