JSP Tag Libraries
Loosely speaking, a collection of custom tags is known as a tag library. Technically, a tag library is all the Java classes for a set of custom JavaServer Pages (JSP) actions, plus a tag library descriptor file that describes their tags to the JSP container. A tag library can be packaged in a JAR file, as the following definition explains. It comes from the document type definition for a tag library descriptor:
- A tag library is a JAR file containing a valid instance of a tag library w descriptor (taglib.tld) file in the META-INF subdirectory, along with the appropriate implementing classes and other resources required to implement the tags defined therein.
JSP custom tags define actions in a manner that is accessible to tools as well as developers. The official way to deliver a tag library to a tool that can use it is to place it as a JAR file in the TOMCAT_HOME\lib folder. A JSP container, such as Tomcat (an open-source server for Java servlets and JavaServer Pages), can also use tag libraries by finding the appropriate implementing classes in its default or other class locations, and locating the tag library descriptor file in a default or other configurable location.
NOTE
You'll notice occasional references to the bonForum tag library. This tag library was developed to explore the use of custom tags in building a browser interface for a multiuser Web chat application. This application was designed as a tool to explore the subjects in my book XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application, while solving some real Web application problems. For more information on the bonForum chat application, check out http://www.bonforum.org.
Taglib Directives
A JSP directive is a type of element that provides global information to the JSP container. Being global, it applies for all the requests that the JSP will service. Most directive information is useful to the container at page translation or compilation time. The syntax of a directive is as follows:
<%@ directive { attr="value" }* %>
The curly brackets and the asterisk simply mean that 0 to N attributes may be present. There may be optional whitespace after <%@ and before %>.
A taglib directive in a JSP document links it to an XML document that describes a set of custom JSP tags and determines which Tag Handler class implements the action of each tag. Here is an example of a taglib directive, taken from the Jakarta-taglibs project:
<%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0" prefix="dt" %>
Here is another taglib directive, the one used by the bonForum Web application:
<%@ taglib uri="http://www.bonForum.org/taglib/bonForum-taglib-0.5" prefix="bon" %>
A taglib directive uses a URI to uniquely identify a tag library to the JSP container (in this case, Tomcat). The directive also tells the JSP container something important: the prefix that the tags in the library will use on this particular JSP document. The given prefix must appear before the tag name that appears in the descriptor file. You can see an example of a prefix in use in the section "Tag Library Descriptor File" later in this article. Prefixes enable you to use tags from different tag libraries without problems arising from clashing names. You use different prefixes (of your choice) in the taglib directives for different libraries on the same JSP.