Custom Tags in JSP 2.0

Custom tag allows programmer to hide code in a Java class and access it from a tag in JSP. This article shows how to create custom tags using JSP 2.0, which has simplified the process of creating custom tags compared with previous version 1.2.

Steps

The following steps are to be taken to create a custom tag.

Creating a simple Tag

Our first task is creating a simple tag. This is mainly to understand the overall process involved in creating a custom tag in JSP 2.0.

Tag Handler

Tag handler is a Java class that responds to events raised by container when custom tag is encountered. Every Tag handler must implement Tag interface or extend TagSupport class provided by JSP API.

The following tag handler is used to respond to doTag() method and sends system date and time back to JSP in which it is placed. WEB-INF/classes/st/CurrentTime.java

package st;

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

public class CurrentTime extends SimpleTagSupport
{

 public void doTag() throws JspException , IOException
 {

   JspWriter out = getJspContext().getOut();
   out.println( new Date().toString());
 }
} // end of class
CurrentTime class extends SimpleTagSupport, which is implementing SimpleTag interface.

Compile tag handler by first setting path and classpath as follows:

path c:\jdk1.4.2\bin;c:\windows\system32
set classpath=.;c:\tomcat5\common\lib\servlet-api.jar;c:\tomcat5\common\lib\jsp-api.jar;
Note: You have to change path of JDK and Tomcat according to your system.

SimpleTag interface contains the following other methods:

We have used doTag(), which is invoked when Container encounters tag in JSP.

Creating Tag Library Descriptor

TLD file is an XML file containing details of tags. TLD file can be placed any where within WEB-INF indirectory.

The following TLD file provides details regading the library such as its version and the version of JSP it needs etc and tags.

WEB-INF/st.tld

<?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/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>st</short-name>
  <tag>
     <name>time</name>
     <tag-class>st.CurrentTime</tag-class>
     <body-content>empty</body-content>
  </tag>       
  
  
  <tag>
       <name>formattime</name>
       <tag-class>st.FormatCurrentTime</tag-class>
       <body-content>empty</body-content>
       <attribute>
          <name>format</name>
          <required>false</required>
       </attribute>
    </tag>       
  
</taglib>

Registering Tag

Before a custom tag is used, its tag library descriptor (TLD) is to be registered in JSP using taglib directive. taglib directive specifies the name of TLD file and prefix to be used while using the tags in the library.

The following JSP registers st.tld and uses time tag with prefix st.

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

<st:time/>

Creating a Tag with Attributes

Let us now modify time tag to support an attribute - format. Format attribute takes the format in which date and time is to be displayed. It is an optional attribute. If it is not given then date and time are displayed in default format.

In order to provide an attribute, we have to modify tag handler to incude a property with the same name as the attribute. If user provides a value to property format then its value is used in format method of SimpleDateFormat class. The following is the new code with a property and extra process added.

FormatCurrentTime.java


package st;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class FormatCurrentTime extends SimpleTagSupport
{
 private String format=null;

 public void setFormat(String format)
 {
    this.format = format;
 }

 public void doTag() throws JspException , IOException
 {

   JspWriter out = getJspContext().getOut();
   if ( format != null)
   {
	   SimpleDateFormat sdf = new SimpleDateFormat( format);
	   out.println( sdf.format( new Date()));
   }
   else
       out.println( new Date().toString());

 }

} // end of class
In order to add an attribute to a tag, modify TLD to include details regarding format attribute as shown below. Tag element is to be added after the previous tag. The complete code is shown below.

   <?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/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
   <taglib>
     <tlib-version>1.0</tlib-version>
     <jsp-version>2.0</jsp-version>
     <short-name>st</short-name>
     <tag>
        <name>time</name>
        <tag-class>st.CurrentTime</tag-class>
        <body-content>empty</body-content>
     </tag>       
     
     
     <tag>
          <name>formattime</name>
          <tag-class>st.FormatCurrentTime</tag-class>
          <body-content>empty</body-content>
          <attribute>
             <name>format</name>
             <required>false</required>
          </attribute>
       </tag>       
     
   </taglib>

Attribute element specifies the details of attributes like name of the attribute and whether or not the attribute is mandatory. If required element is set to true then attribute becomes mandatory.

The following is the code for time.jsp. It shows how to use time tag and also formattime tag.


<%@ taglib uri="/WEB-INF/tlds/st.tld" prefix="st"%>
<st:time/>
<p>
<st:formattime />
<p>
<st:formattime format="dd/MMM/yy"/>

P.Srikanth.