[ 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;}
}

CAUTION

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"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>
 <tlib-version>1.0</tlib-version>
 <jsp-version>1.1</jsp-version>
 <short-name>wl</short-name>
 <uri>http://www.beasys.com/j2ee/tlds/wl-jsptaglibrary_1_0.tld</uri>

 <tag>
  <name>greetings</name>
  <tag-class>com.tags.Greetings</tag-class>
  <bodycontent>empty</bodycontent>
  <description>Displays Greeting</description>
 </tag>

</taglib>

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:


<taglib>
   <taglib-uri>
    greetings.tld
   </taglib-uri>
   <taglib-location>
     /WEB-INF/lib/greetings-tags.jar
   </taglib-location>
</taglib>

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">

<html>
<head>
<title>Greetings</title>
</head>
<body>
    <greet:greetings />
</body>
</html>

The resulting browser display is shown in Figure 18.1.

Figure 18.1. Browser view of Greetings.jsp.

graphics/18fig01.gif

    [ Team LiB ] Previous Section Next Section