|[ Team LiB ]|
A very powerful feature of JavaServer Pages technology is the ability to create your own XML tags for use in JSPs. Custom tags have been a part of the JSP specification since Version 1.1. JSP 2.0 is dedicated to making custom tag development less complex than prior versions. JSP 2.0's introduction of simple tag handlers and tag files, which we cover in Recipe 22.8-Recipe 22.14, are a big part of this strategy.
Let's familiarize ourselves with a few terms before we move on to custom tag recipes. A tag is an instance of an XML element and a member of a specified namespace. For example, the prefix for all tags associated with this cookbook is cbck. The JSP refers to an individual tag associated with the cbck namespace (say, the myTag tag) as follows:
<cbck:myTag>whatever this tag does...</cbck:myTag>
Tags are XML elements; therefore, their names and attributes are case sensitive. A collection of tags that provide similar functionality or that logically collaborate with each other is called a tag library. Developers can install one or more tag libraries in a web application.
A Java class called the tag handler provides the tag's functionality in a JSP. A custom action is a tag that you invent for use in JSPs and that is powered behind the scenes by a tag-handler object that the web container keeps in memory.
A tag file defines a custom tag in JSP syntax. It is designed to make life easier for tag developers. The web container generates from the tag file a Java class that implements the SimpleTag interface, and then creates an object from that class to interpret the tag's use in JSPs.
Finally, a tag library descriptor (TLD) is an XML file that provides a mapping between references to tag libraries in JSPs (with the taglib directive) and the tag-library classes that you install in the web application. A TLD is a configuration file, similar to a web application's deployment descriptor. The recipes in this chapter provide examples of how to create classic tag handlers, simple tag handlers, and tag files. The recipes also show how to package these components in web applications.
|[ Team LiB ]|