Chapter 9) Using Tag Libraries

9.1) For a custom tag library or a library of Tag Files, create the 'taglib' directive for a JSP page.

This objective was addressed briefly in objective 6.6. The taglib directive for a JSP page takes the format.

<%@ taglib uri="URIForLibrary" prefix="tagPrefix" %>

The URI can take the form of either pointing directly to the tag library descriptor file or it can point to a URI within the deployment descriptor (WEB.XML) that then points to the tag library descriptor file. It is generally considered more flexible to use a uri that maps to a uri within the deployment descriptor.

The following taglib directive shows how the uri can point directly to the tag library descriptor file.

<%@ taglib uri="WEB-INF/taglib.tld" prefix="h" %>

Wrapping a taglib in a Jar

As was shown with the JSTL it is common to put taglibs in a jar file once the development process has finished. This gives the simplicity of being able to deliver the functionality of multiple taglibs in a single file rather than needing to copy multiple files to multiple directories. The following example shows how a TagLib called SimpleTagDemo can be wrapped in a Jar and made available to a JSP page. Don't worry about the actual code of the taglib, it is covered in more detail in section 10.4. The file contains the following code.

* @author Marcus Green
 * May 2006
 * A trivial example of
 * a simple tag
package com.examulator;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class SimpleTagDemo extends SimpleTagSupport {
    public void doTag() throws JspException{
        PageContext context = (PageContext) getJspContext();
       JspWriter out = context.getOut();
        try {
            out.println("SimpleTagSupport demo");
        } catch (Exception e) {/* log the error */}        

The file was created\copied to the directory


And compiled from directory


with the command line

javac com\examulator\

The output file SimpleTagDemo.class was copied to


A file called taglib.tld was created in the directory


with the following text.

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns=""
xsi:schemaLocation=" web-jsptaglibrary_2_0.xsd">
        <info>Demonstrates use of the SimpleTagSupport class</info>   

The class file and its tld were wraped in a jar file with the following command executed from the webapp directory

jar -cvf SimpleTagDemo.jar META-INF\*

The resulting jar file can be deployed by copying it to the \webapp\WEB-INF\

The taglib can be directly access in the following manner in a jsp page.

<%@ taglib uri="WEB-INF/lib/SimpleTagDemo.jar" prefix="h" %>
<h1>Here we go </h1>

It could also be deployed by adding an entry into the deployment descriptor that pointed to the jar file.

The Taglib prefix

The prefix attribute is mandatory and must be unique within the page, i.e. You could not have two taglib directives using the prefix h within the same page even if they each had a different URI.

Instead of the uri attribute the tagdir attribute can be used. If the tagdir attribute is used it must point to a directory starting with "/WEB-INF/tags/. Thus the following is a valid taglib directive that uses tagdir.

<%@ taglib tagdir="/WEB-INF/tags/taglib.tld" prefix="h" %>

URI reserved words

The following words are reserved and cannot be used as a prefix for a taglib jsp:, jspx:, java:, javax:, servlet:, sun:, and sunw

Other sources

The taglib directive according to Sun

According to Mikalai Zaikin

Last modified: Sunday, 20 September 2015, 07:21 PM