Packaging and Installing a Tag Library

There are basically two ways the files that make up a tag library (the TLD and all the class files) can be made available to a container: packaged in a JAR file or kept as regular files in the filesystem. On top of this, there are three ways to identify the tag library you use in a JSP page. Let’s look at the two topics one at a time.

Making the Tag Library Files Available to the Container

During development, you may want to let the tag library classes and the TLD file reside as-is in the filesystem, since it makes it easy to change the TLD and modify and recompile the classes. Just make sure the class files are stored in a directory that’s part of the classpath for the JSP container, such as the WEB-INF/classes directory for the web application. The TLD must be also available as a file with a .tld extension in a directory where the JSP container can find it. The recommended location is the WEB-INF/tlds directory.

When you’re done with the development, you may want to package all tag-handler classes and the TLD in a JAR file. This makes it easier to install the library in an application. In this case, the TLD must be placed as a file with a .tld extension in the META-INF directory in the JAR file, for instance as META-INF/taglib.tld.

To create the JAR file, first arrange the files in a directory with a structure like this:

META-INF/
  taglib.tld
com/
  ora/
    jsp/
      tags/
        ForwardTag.class
        ...
      util/
        StringFormat.class
        ...

The structure for the class ...

Get JavaServer Pages, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.