One of the most exciting features about JSP is the ability tobuild and use custom "tag libraries" in your JSP applications. In thisarticle, find out why tag libraries are a Good Thing, and read about how toget and install custom tag libraries for common tasks.
A tag library is made up of several components. First comes the tag class itself, written in Java - this contains all the behind-the-scenes code which makes the tag function. This class will be referenced in the tag library descriptor, and constitutes the meat of the sandwich we're building.
Next, you need a "tag library descriptor", an XML file usually identified with the .TLD file extension. This TLD contains information on different aspects of the tag library: the name of the tag, and of the Java class associated with it, a short description of functionality, and optional attributes and body content.
Both these components are usually packaged into a JAR file for easy distribution.
Now, we're not going to get into the nitty-gritty of writing a tag library here - there are already a whole bunch of tutorials which discuss this in detail, and links to some are included at the end of this article. What we will do, however, is illustrate how a tag library can be incorporated into your JSP document, and how it interacts with the rest of your script.
In order to illustrate this, we'll be using the Jakarta Project's DATETIME tag library, designed to manipulate date- and time-stamps. You can download a copy of this library from http://jakarta.apache.org/taglibs/doc/datetime-doc/intro.html, and you can find a number of other freeware tag libraries at http://www.jsptags.com/, one of the larger repositories on the Web.
Once you've downloaded a copy of the library, you need to tell Tomcat about it before you can begin using it. The first step here is to decide the context within which you plan to use the library - for purposes of this example, we will assume the context is "/test/". Next, copy the TLD file from the distribution into the context's "web-inf/" directory, and the main JAR file into the context's "web-inf/lib/" directory.
The last step here is to open up the "web.xml" file which resides in the "web-inf/" directory, and alter it to reflect the new tag library - this is accomplished via the <taglib> directive.