How do I use the taglib directive to add custom tags to JSP?

Simon Brown

Custom tags are pretty straightforward to build, and a useful tutorial can be found at orionserver.com.

However, this is (of course) geared up to using their own server, so here's an example of how to build a custom tag using the reference implementation of the Servlet/JSP specifications - Tomcat. For simplicity, this example will make use of the example web application that comes with Tomcat (TOMCAT_HOME/webapps/examples).

Step 1 : Build a class that implements the javax.servlet.jsp.tagext.Tag interface as follows. Compile it and place it under the web-inf/classes directory (in the appropriate package structure).

package examples;

import java.io.*;

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

public class ShowDateTag implements Tag {
	
	private PageContext pageContext;
	private Tag parent;
	
	public int doStartTag() throws JspException {
		return SKIP_BODY;
	}
	
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().write("" + new java.util.Date());
		} catch (IOException ioe) {
			throw new JspException(ioe.getMessage());
		}
		
		return EVAL_PAGE;
	}
	
	public void release() {
	}
	
	public void setPageContext(PageContext page) {
		this.pageContext = page;
	}
	
	public void setParent(Tag tag) {
		this.parent = tag;
	}
	
	public Tag getParent() {
		return this.parent;
	}
	
}
Step 2 : Now we need to describe the tag, so create a file called taglib.tld and place it under the web-inf directory.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" 
  "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>
  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>myTag</shortname>
  <uri>http://www.mycompany.com/taglib</uri>
  <info>My own tag library</info>

  <tag>
    <name>showDate</name>
    <tagclass>examples.ShowDateTag</tagclass>
    <info>Show the current date</info>
   </tag>       

</taglib>
Step 3 : Now we need to tell the web application where to find the custom tags, and how they will be referenced from JSP pages. Edit the web.xml file under the web-inf directory and insert the following XML fragement.
    <taglib>
        <taglib-uri>
	   http://www.mycompany.com/taglib
        <taglib-uri>
        <taglib-location>
           /WEB-INF/taglib.tld
        <taglib-location>
    </taglib>
Step 4 : And finally, create a JSP page that uses the custom tag.
<html>

<head><title>Date tag example</title></head>

<body> 
<%@ taglib uri="http://www.mycompany.com/taglib" prefix="myTag" %>

<myTag:showDate/>
</body>
</html>
Now restart the server and call up the JSP page! You should notice that every time the page is requested, the current date is displayed in the browser.

Whilst this doesn't explain what all the various parts of the tag are for (e.g. the tag description, page context, etc) it should get you going. If you use the tutorial (above) and this example, you should be able to grasp what's going on!
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact