Java Reference
In-Depth Information
methods requiring that the developer only implement the methods that apply to their specific
tag. For a tag without a body the helper is
TagSupport
; therefore if you are developing a tag
handler for a tag with no body, your handler needs to extend the
TagSupport
class. The meth-
ods that are implemented by the
TagSupport
class are listed in Table 19.2.
T
ABLE
19.2
Methods Implemented by the
TagSupport
Class
Method
Definition
Sets the
PageContext
associated with the current page.
setPageContext()
Sets the reference to the parent
Tag
, if the current
Tag
is nested.
setParent()
Called when the JSP container encounters the beginning of this tag.
doStartTag()
Called when the JSP container encounters the ending of this tag.
doEndTag()
Called by the container when the tag is finished processing. It
should be used to reset all data members and free all allocated
resources. The
release()
method is guaranteed to be invoked only
once.
release()
Before you can create your own bodiless tag, you need to understand the actions taken by the
JSP container when it does encounter a tag without a body. After the container has identified
the tag handlers associated with the encountered tags, as described in the steps in the preceding
section “Adding the
taglib
Directive to Your JSP,” the JSP container continues with the fol-
lowing steps. These steps apply only to tags that have no body and no attributes:
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.
19
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.
4.
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, and
SKIP_PAGE
, which tells the container to stop evaluating the rest of the page.
5.
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.
A Sample Tag with No Body
Now that you have an understanding of how bodiless tags work, let's create an example. Your
custom tag, called the
hello
tag, when encountered will simply output the text
Hello
. The first
step you need to take is to create a class that extends the
TagSupport
class and add the appro-
priate functionality to output your text message. Listing 19.1 contains the source for your tag.