Home > Articles > Programming > Java

The Standard Tag Library in the JSP Specification

  • Print
  • + Share This
Although the standard tag library is not technically a portion of the JSP specification, they are closely related, and can be used in any application server that supports JSPs. Discover what these tags can do, in this sample chapter from JavaServer Pages.
This chapter is from the book

This chapter is from the book

Chapter 3 explained how to get values from beans to pages with the jsp:getProperty tag, along with a number of limitations in this process. There was no good way to display the tracks on a CD, because the page has no way to know how many tracks a bean will be holding. The quiz was unable to determine whether the user’s answer was correct, because the page has no way to compare two values in a bean.

Both of these problems can be solved by a new set of tags: the standard tag library. Although these tags are not technically a portion of the JSP specification, they are closely related and can be used in any application server that supports JSPs. This chapter looks at what these tags can do, after a few words on how tags in JavaServer Pages work in general.

4.1 Tag Libraries

We have already seen tags that deal with things ranging from including other JSPs to manipulating beans. These tags are all useful and perform their specific tasks well, but almost from the beginning, the authors of the JSP specification realized that no set of tags could possibly do everything that everyone would need from JSPs. To address that issue, those authors provided a mechanism for programmers to create new tags that could do anything possible and an easy way for pages to use these custom tags. The topic of the creation of new tags is covered in Chapter 13. Listing 4.1 illustrates how a page loads and uses a tag.

Listing 4.1 A JSP that uses a custom tag

<%@ taglib prefix="awl"
    uri="http://jspbook.awl.com/samples" %>
The time, in two different formats:<p>
<awl:date format="EEEE, MMMM dd yyyy 'at' hh:mm"/><br>
<awl:date format="hh:mm:ss MM/dd/yy"/><br>

The tag library is loaded with the first line. The URI (Uniform Resource Identifier) specifies the location of the tag library definition, and the prefix specifies the name that will be used to access the tags. Here, the prefix is awl, but it could be anything, as long as it is used consistently. One of the tags from this library, time, is used twice in the last two lines. The name of the tag is prepended by the prefix specified at the top.1

The awl:time tag itself simply sends the current time to the page, in a format specified by the format property. If this looks familiar, it is because this does essentially the same thing as Listing 3.2. That example used a bean with an input for the format and an output for the time. Using a custom tag, the input is specified as a named property, and the output is implicit in the way the tag works.

Technically, neither example was particularly good. Because they play the part of models in the model/view/controller paradigm, beans should not be concerned with how their data will be presented. Hence, the bean used in Listing 3.2 should not have had to deal with formatting issues. Similarly, tags are intrinsically part of the view portion and so should not deal directly with data, but the awl:time tag in Listing 4.1 holds data in the form of the current time. With some effort, the standard tag library can help make such separations of roles between tags and beans easier to manage, as will be seen later in this chapter.

  • + Share This
  • 🔖 Save To Your Account