[ Team LiB ] Previous Section Next Section

Recipe 22.5 Packaging the Tag Library in a JAR File

Problem

You want to make your tag library available in a JAR file.

Solution

Create a JAR file that contains your tag handler class or classes in the correct directory structure (with subdirectory names matching the package names). Place the tag library descriptor file in the JAR's META-INF directory. Then put the JAR in the WEB-INF/lib directory of your web application.

Discussion

To make your tag library portable, store all of your tag handler classes and tag files in a JAR file.

In a JAR, store any tag files in META-INF/tags or a subdirectory of META-INF/tags. If you don't, the JSP container will not recognize them as legitimate tag files. See Recipe 22.11 for details.


You can generate this JAR file from a directory that contains your tag library classes, including their package-related subdirectories. For example, the logo tag I developed in this chapter has a package name of com.jspservletcookbook, so the relative path to this file is com/jspservletcookbook/LogoTag.class. Include a META-INF directory at the top level of the directory where the classes are stored (e.g., in the same directory as the one containing com). Place your tag library descriptor file in the META-INF directory or a META-INF subdirectory.

If your library includes any tag files, place them in META-INF/tags or a subdirectory of META-INF/tags. Change to the directory containing all these subdirectories and type the following command line, substituting your own JAR filename for cookbooktags.jar:

jar cvf cookbooktags.jar .

Don't forget that period (.) character at the end. This tells the jar tool to include all of the files and directories that the current directory contains in the archive.

Make sure your computer's PATH environment variable includes the path to the bin directory of your Java SDK installation, as in h:\j2sdk1.4.1_01\bin. This allows you to type jar at the command line to launch the Java jar tool.


To install the tag library, just take the resulting JAR file and move it into your web application's WEB-INF/lib directory.

The JSP container (in JSP 1.2 and 2.0) automatically looks in the JAR's META-INF directory for the TLD file—you do not have to include a taglib element in the web.xml deployment descriptor.

See Also

The JSP 2.0 specification web page: http://jcp.org/en/jsr/detail?id=152; Recipe 22.2 and Recipe 22.3 on creating TLD files for tag libraries; Recipe 22.4 on packaging a tag library in a web application without using a JAR file; Recipe 22.6 on using the custom tag in a JSP; Recipe 22.7 on handling exceptions in tags; Recipe 22.8 and Recipe 22.9 on creating a simple tag handler; Recipe 22.10 on using the simple tag handler in a JSP; Recipe 22.11 and Recipe 22.14 on using a JSP tag file; Recipe 22.15 on adding a listener class to a tag library; the custom tag sections of Hans Bergsten's JavaServer Pages, Third Edition (O'Reilly).

    [ Team LiB ] Previous Section Next Section