|
JSP Tag Java Interface
Part:
1
2
3
(Continued from previous part...)
Implenting BodyTag Interface - TraceTag.java
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 doAfterTag()
to force the tag body being evaluated twice.
/**
* TraceTag.java
* Copyright (c) 2003 by Dr. Herong Yang. 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\jakarta-tomcat-4.1.18\webapps\root\web-inf\classes\herong directory.
I had to put it under subdirectory "herong", because the tag class was
define 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) 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>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\jakarta-tomcat-4.1.18\webapps\root\web-inf\tlds directory.
To test my trace tag, I wrote the following JSP page, TraceTagTest.jsp:
<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="1.2">
<!-- TraceTagTest.jsp
Copyright (c) 2003 by Dr. Herong Yang
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<hy:trace myAtt="my value">
<jsp:text>JSP body</jsp:text><br/>
</hy:trace>
</body></html>
</jsp:root>
Note that I did not use jsp:directive.page to import my trace tag class,
because the class in now under "herong" package.
I put the JSP page on my tomcat server, and restarted the server. When
requesting this JSP page with IE, I got the following output:
setPageContext() called.
setParent() called.
setMyAtt() called.
doStartTag() called.
JSP body
doAfterBody() called.
JSP body
doAfterBody() called.
doEndTag() called.
The output was exactly what I expected. The JSP was indeed evaluated twice
as controlled by the returning flag of doAfterBody().
One surpprise is that there was no call to release().
I don't know why.
(Continued on next part...)
Part:
1
2
3
|