JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

JSP Custom Tag

Part:   1  2 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

3. Writing the tag library descriptor (tld) file. Now, I need to define a tag in a tag library descriptor file to use the tag class. Here is my first tld file, HyTaglib.tld:

<?xml version="1.0"?>
<!DOCTYPE taglib PUBLIC 
 "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 
 "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_2.dtd"> 
<!-- HyTaglib.tld
     Copyright (c) 2003 by Dr. Herong Yang
-->
<taglib>
<tlib-version>1</tlib-version>
<jsp-version>1.2</jsp-version> 
<short-name>Herong's Tag Library</short-name> 
<tag>
 <name>hello</name>
 <tag-class>HelloTag</tag-class>
 <body-content>empty</body-content>
</tag>
</taglib>

4. Installing the tld file. Tag library descriptor files need to be accessible by the tomcat server. So I copied HyTaglib.tld to \local\jakarta-tomcat-4.1.18\webapps\root\web-inf\tlds directory.

5. Writing the JSP page. To use my first custom tag, I wrote the following JSP page, hello_tag.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" 
   xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="1.2"> 
<!-- hello_tag.jsp
     Copyright (c) 2003 by Dr. Herong Yang
-->
<jsp:directive.page import="HelloTag"/>
<jsp:directive.page contentType="text/html"/>
<html><body>
<hy:hello/>
</body></html>
</jsp:root>

6. Viewing the JSP page. To see the output of my JSP page, I copied hello_tag.jsp to \local\jakarta-tomcat-4.1.18\webapps\root, started tomcat 4.1.18 server, and use Internet Explorer (IE) to view http://localhost:8080/hello_tag.jsp.

7. I did get the "Hello world!" message in the IE window. So my hello tag worked perfectly.

8. If you are changing your tag class after it has been loaded by tomcat, you may need to restart tomcat, or click "restart" on the "root" application on the tomcat admin page. "root" application is where I put my JSP pages.

How Custom Tag Works

Here is my understanding of how custom tag works, using the "Hello world!" tag as an example:

  • When "hello_tag.jsp" page is requested for the first time, Tomcat server will translate the JSP page into a java class.
  • When the <hy:hello/> custom tag is encountered during the translation, Tomcat server will follow the tld file to locate the HelloTag.class file. Note that the tld file is provided in the "jsp:root" element.
  • Then Tomcat server will replace the custom tag with some Java code to instantiate an object of the tag class, initialize the object, and call the doStartTag() method.
  • To output data into page, you can get an output stream from the pageContext object provided by the JSP tag extension facility, pageContext.getOut(). This is how the "Hello world!" message gets produced in the IE window.
  • Note that I have to use "jsp:directive.page" to import HelloTag class into the JSP page, because my HelloTag class has no Java package name and needs to be imported, even if it is located in the class path.

Part:   1  2 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - JSP Custom Tag