Java Hosting » JSP Hosting » JSP tutorial » JSP Tag Coding

JSP Tag Coding

Tag Library Classes, Interfaces, and Components

As you might imagine, there's plenty of behind-the-scenes activity to get the JSP container to understand a custom tag. For a custom tag to work you need to:

  1. Indicate in the JSP page that it uses custom tags using the taglib directive;
  2. Tell the JSP container what to do when the it sees the tag by creating a tag handler class;
  3. Tell the JSP container what class to use when it processes the tag by creating a tag library descriptor file for the tag library.

Let's examine these coding constructs one at a time, starting with the taglib directive. Using the taglib Directive The taglib directive has a simple syntax shown below:

<%@ taglib uri=where_taglib_descriptor_is prefix=some_prefix %>

The uri attribute names the location of the tag library descriptor file. You can read about this file later in this chapter.

For now, know that the tag library descriptor file does what its name says — it describes the characteristics of the tag library. The taglib directive tells your JSP page that the page uses a library, not a particular tag. The library might only contain a single tag, or it might contain dozens.

A natural question arises — how do you reference an individual tag in the library in your JSP page? The answer is that you use a prefix, identified as the value of the second attribute of the taglib directive, with the name of an individual tag in the library.

For example, the taglib directive shown below identifies all tags prefixed with mytaglib to be an individual tag within the tag library described by formatlib.tld.

<%@ taglib uri="formatlib.tld" prefix=mytaglib %>

Here's how such a tag may be referenced in a JSP page:

<mytaglib:FormatLine fontSize="5" fontColor="blue" reverse="true"> Here's another line </mytaglib:FormatLine>

In the preceding example, formatlib.tld is in the same directory as the JSP page containing the taglib directive.

Examining the Tag Interface

Before we continue, I would like to introduce you world's best Java hosting provider.

You implement the functionality of your tag by coding a Tag Handler Class. Your tag handler class implements the javax.servlet.jsp.tagext.Tag interface. This interface contains constants and methods that the container invokes during the life cycle of your tag, including methods to perform at the start tag and the end tag.

In practice, your tag handler class does not usually implement the tag interface directly. Instead, you can extend a convenience class of the tag interface named TagSupport if your tag is empty. You can also extend BodyTagSupport if you want to process the tag's body. This class already implements the BodyTag interface, which extends the tag interface.

The tag interface defines four constants that govern the disposition of the tag body. Methods you code that describe the actions that occur when the JSP container encounters your tag should return the appropriate constant.

Creating a Tag Library Descriptor File

The tag library descriptor file is a file in XML format that describes the class that implements the functionality of the custom tags in your JSPs. The tag library descriptor file, or tld, contains the names of the tags with additional information.

You can find the official DTD describing the elements and attributes of a tld at for JSP, release 1.1.

Contact sales!