Java Reference
In-Depth Information
L
ISTING
19.4
Continued
super.release();
}
}
As you look over this tag, you will see that it first extends the
BodyTagSupport
class. This is
the helper class for tags with bodies. It provides default implementations for the methods
defined in the
BodyTag
interface.
The only difference from the
HelloTag's doEndTag()
method is the following code snippet:
// 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);
In the
HelloBodyTag's doEndTag()
method you first get the
String
representation of the tag's
body using the
bodyContent.getString()
method. After you have this
String
you get a refer-
ence to a
JspWriter
that references the current stream being used for client response, as you
did in your
HelloTag
. You then use the
print()
method to write the text
Hello
string plus the
text value in the body of your
HelloBodyTag
to the response stream.
To see this tag in action you need to first compile the
HelloBodyTag.java
file and move the
class file into the
<SERVER_ROOT>/webapps/djs/WEB-INF/classes/com/djs/
directory.
You then need to add the following code snippet to your
tld
:
19
<tag>
<name>helloBody</name>
<tagclass>com.djs.HelloBodyTag</tagclass>
<bodycontent>JSP</bodycontent>
<info>Just Says Hello to the Body</info>
</tag>
The most important thing to notice in the above snippet is the
bodycontent
for this tag. It is
now
JSP
instead of
empty
. This tells the container that the tag body can have content and this
content can be JSP code.
Now you can see your new body tag in action. Listing 19.5 contains the source for the JSP that
will use your
helloBody
tag.