[ Team LiB ] Previous Section Next Section

Overview of the Steps Required to Build JSP Tags

Implementing Java classes called tag handler classes creates JSP tags. Tag handler classes implement one of two interfaces:

javax.servlet.jsp.tagext.Tag or javax.servlet.jsp.tagext.BodyTag. The Tag interface is implemented for simple JSP tags—those JSP tags without an accompanying tag body. Simple JSP tags are also referred to as empty body tags. The BodyTag interface implements tags with an accompanying tag body. Tags can also be implemented by using the abstract class javax.servlet.jsp.tagext.TagSupport or javax.servlet.jsp.tagext.BodyTagSupport. Use of these classes enables the developer to avoid implementing methods that aren't used.

Once the tag handler is implemented, the tag library descriptor (TLD) must be created. The tag library descriptor defines the tag library and provides critical information about the tag, such as the name of the tag handler class and attributes, if any, among other issues. For detailed information about creating the tag library descriptor, refer to http://java.sun.com/j2ee/tutorial and http://edocs.beasys.com/wls/docs81/taglib. After the tag library descriptor is created, use the custom tags to register and deploy the tag library as detailed in the "JSP Tag Library Syntax" section of Chapter 17, "Using JSP Tag Libraries." For example, to create a tag that presents a greeting, we would first implement the TagSupport class as shown in the Greetings.java file in Listing 18.1.

Listing 18.1 Greetings.java
package com.tags;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class Greetings extends TagSupport
  public Greetings() {}

  public int doStartTag() throws JspException {
    try {
         pageContext.getOut().println("<h1>Greetings All</h1>");
    catch (Exception ex) {
         throw new JspException("Greetings Tag:Exception: " + ex.getMessage());
   return SKIP_BODY;
   public int doEndTag() {return EVAL_PAGE;}


Ensure that you define a package for your class. Tag handlers without package structures might not be found during JSP execution.

Compile the tag support class using your Java compiler. Use the -d option to build your package directory (com.tags in our example) and then create the TLD as follows:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"


  <description>Displays Greeting</description>


Now place the TLD within a META-INF subdirectory and JAR the tag class and TLD using the command line as follows:

$ jar cvf greetings-tags.jar com META-INF

Note that the directory com\tags contains your compiled tag support class.

Now deploy the tag library to the WEB-INF/lib directory of the target Web application, and register the library within the Web application's WEB-INF/web.xml file as shown here:


The tag library is now available for use. To use the tag library, code a JSP by declaring the tag library and using the custom tag as shown here:

<%@ taglib uri="greetings.tld" prefix="greet" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <greet:greetings />

The resulting browser display is shown in Figure 18.1.

Figure 18.1. Browser view of Greetings.jsp.


    [ Team LiB ] Previous Section Next Section