Java Reference
In-Depth Information
A Sample Tag with a Body
Let's create a tag with a body. You will use a tag much like your Hello tag. The difference is
that your BodyTag will print the text Hello and the content of the tag. Your new tag will be
named HelloBodyTag . Listing 19.4 contains the source for this tag.
L ISTING 19.4
HelloBodyTag.java
package com.djs;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class HelloBodyTag extends BodyTagSupport {
public HelloBodyTag() {
}
// Method called when the closing hello tag is encountered
public int doEndTag() throws JspException {
try {
// Get the content of this tag's body
String name = bodyContent.getString();
// We use the pageContext to get a Writer
// We then print the text string Hello + the content
// of the tag's body.
pageContext.getOut().print(“Hello “ + name);
}
catch (Exception e) {
throw new JspTagException(e.getMessage());
}
// return SKIP_BODY, because we will not evaluate the body again.
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.
Search WWH ::




Custom Search