Java Reference
In-Depth Information
Now whenever you add the text
<djs:hello />
to your JSP, it will be replaced by the text
Hello
.
Tags with Bodies
Now that you have seen a custom tag without a body, let's create a tag that uses a body. All tag
handlers with bodies must implement the
BodyTag
interface. The
BodyTag
interface extends the
Tag
interface by adding functionality to handle body content. It does this by adding three new
methods. Each one of these methods is described in Table 19.3.
T
ABLE
19.3
Methods Added by the
BodyTag
Interface
Method
Definition
Used by the container to set the current content of the tag's body.
setBodyContent()
Executed once per invocation after the
setBodyContent()
method
and before the evaluation of the tag's body.
doInitBody()
Called by the container after the body content has been evaluated.
doAfterBody()
Like tags without bodies, tags with bodies also have a helper class. Their helper is
BodyTagSupport
and it extends
TagSupport
and implements the
BodyTag
interface.
When the JSP container encounters a tag with a body it performs the following steps:
1.
The
setPageContext()
method is called, setting the current page's
PageContext
.
2.
The
setParent()
method is called with a reference to the parent
Tag
object, if this is a
nested tag, or
null
if not.
3.
The
doStartTag()
is then called. It will return either
EVAL_BODY_INCLUDE
or
SKIP_BODY
.
If
EVAL_BODY_INCLUDE
is returned, the container should evaluate the body of the tag. If
SKIP_BODY
is returned, the container will not evaluate the body.
19
4.
The
setBodyContext()
is then called.
5.
The
doInitBody()
is then called.
6.
The
doAfterBody()
is then called repeatedly as long as it returns
EVAL_BODY_TAG
.
7.
The
doEndTag()
method is called when the closing tag element is encountered. It can
return either of two values:
EVAL_PAGE
, which tells the container to continue evaluating
the page, or
SKIP_PAGE
, which tells the container to stop evaluating the rest of the page.
8.
The final step performed by the container is to call the
release()
method. This method
is used to reset class level attributes and to release any allocated resources.