JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
IterationTag Interface Test Class - TraceTag.java
This section provides a tutorial example on how to write a Java tag class that implements the javax.servlet.jsp.tagext.IterationTag interface.
In order to confirm my understanding of the IterationTag interface, I wrote the following tag class to print a short message from each implemented method to show when it is called. I also used a very simple logic in doAfterBody() to force the tag body being evaluated twice.
/* TraceTag.java * Copyright (c) 2002 HerongYang.com. All Rights Reserved. */ package herong; import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class TraceTag implements IterationTag { private boolean stop = false; private PageContext pc = null; private Tag t = null; public void setPageContext(PageContext pc) { this.pc = pc; println("setPageContext() called."); } public void setParent(Tag t) { this.t = t; println("setParent() called."); } public Tag getParent() { println("setParent() called."); return t; } public void setMyAtt(String v) { println("setMyAtt() called."); } public int doStartTag() { println("doStartTag() called."); return EVAL_BODY_INCLUDE; } public int doAfterBody() { println("doAfterBody() called."); if (!stop) { stop = true; return EVAL_BODY_AGAIN; } else { return SKIP_BODY; } } public int doEndTag() { println("doEndTag() called."); return EVAL_PAGE; } public void release() { println("release() called."); } private void println(String s) { try { pc.getOut().println(s+"<br/>"); } catch (IOException e) { System.err.println(e.toString()); } } }
In this tag class, I was planning to have one attribute for my trace tag. Attributes of a tag need to be implemented as properties in the tag class. The setMyAtt(String v) method define a property called "myAtt" for my trace tag.
After compiling my trace tag class, I copied TraceTag.class to \local\tomcat\webapps\ROOT\WEB-INF\classes\herong directory. I had to put it under subdirectory "herong", because the tag class was defined in "herong" package.
Then I updated my tld file, HyTaglib.tld, with a new tag element:
<?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) 2006 HerongYang.com. All Rights Reserved. --> <taglib> <tlib-version>1</tlib-version> <jsp-version>2.1</jsp-version> <short-name>Herong's Tag Library</short-name> <tag> <name>trace</name> <tag-class>herong.TraceTag</tag-class> <body-content>jsp</body-content> <attribute> <name>myAtt</name> <required>false</required> </attribute> </tag> <!-- other tags --> </taglib>
Since my trace tag will have body, I set body-content to "jsp" instead of "empty". The updated TLD file was copied to \local\tomcat\webapps\ROOT\WEB-INF\tlds directory.
Restart the Tomcat server. Now my "hy:trace" tag is ready to use in JSP pages.
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
Non-ASCII Characters Support in JSP Pages
Overview of JSTL (JSP Standard Tag Libraries)
javax.servlet.jsp.tagext.* Package
javax.servlet.jsp.tagext.IterationTag Interface
►IterationTag Interface Test Class - TraceTag.java
IterationTag Interface Test JSP - TraceTagTest.jspx
Servlet Class Converted from JSP - TraceTagTest_jsp.java
TagSupport Class - Dummy Implementation of IterationTag
Multiple Tags Working Together
Using Tomcat on CentOS Systems
Connecting to SQL Server from Servlet