Java Reference
In-Depth Information
L
ISTING
19.1
HelloTag.java
package com.djs;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
public class HelloTag extends TagSupport
{
public void HelloTag() {
}
// Method called when the closing hello tag is encountered
public int doEndTag() throws JspException {
try {
// We use the pageContext to get a Writer
// We then print the text string Hello
pageContext.getOut().print(“Hello”);
}
catch (Exception e) {
throw new JspTagException(e.getMessage());
}
// We want to return SKIP_BODY because this Tag does not support
// a Tag Body
return SKIP_BODY;
}
public void release() {
// Call the parent's release to release any resources
// used by the parent tag.
// This is just good practice for when you start creating
// hierarchies of tags.
super.release();
}
}
As you look over this tag, you will see that it first extends the
TagSupport
class. This is the
helper class for tags without bodies. It provides default implementations for the methods
defined in the
Tag
interface. The next thing you should notice is the two methods that are over-
ridden from the
TagSupport
class:
doEndTag()
and
release()
.