The Tag Handler is responsible for the interaction between the JSP page and
additional server-side objects. The handler is invoked during the execution
of a JSP page when a custom tag is encountered. The doStartTag()
and doEndTag() methods are invoked when the start and end custom
tags, respectively, are encountered. The release() method releases
resources allocated by the tag handler.
There are two interfaces that describe a tag handler:
Tag |
used for simple tag handlers not interested in manipulating
their body content |
BodyTag |
an extension of Tag and gives the handler access to its body |
The Tag Handler has two main action methods:
doStartTag() |
process the start tag of this action.
|
doEndTag() |
process the end tag of this action. Called after
returning from doStartTag. |
release() |
release resources |
doStartTag () returns the following:
EVAL_BODY_INCLUDE
- process the body of the action but do not create a new BodyContent. Pass the body through without manipulating it. Only valid if you DON'T implement the
BodyTag interface.
EVAL_BODY_TAG
- process the body of the action and create a new BodyContent. Only valid if you DO implement the
BodyTag interface.
SKIP_BODY
- do not evaluate the body of the tag
doEndTag () returns the following:
EVAL_PAGE
- the rest of the JSP page will be evaluated
SKIP_PAGE
- the rest of the JSP page will not be evaluated
The return values direct the JSP container on how to evaluate the rest of the
JSP page. The release() method releases resources allocated by
the tag handler.
TagSupport and BodyTagSupport are subclasses of Tag
and can be used as base classes when creating new tag handlers.
The TagSupport class is a utility class that implements the Tag
interface and adds additional convenience methods including:
- getter method for
Tag properties
If the tag handler manipulates the body of an action, it must implement the
BodyTag interface. doStartTag () must return EVAL_BODY_TAG
in order for the body of the tag to be evaluated. If SKIP_BODY
is returned, the body will be ignored. Methods that interact with the body content
include:
doInitBody () |
invoked before the body of the tag is evaluated but after
body content is set |
doAfterBody () |
invoked after body content is evaluated |
The BodyTagSupport class implements the BodyTag
interface and adds additional convenience methods. Some of these methods include:
- getter for the
bodyContent property
- getter for the previous
out JSPWriter
In a Web Application handlers must reside in one of the following standard
locations for Java classes:
- in a JAR file in the
/WEB-INF/lib directory
- in a directory in the
/WEB-INF/classes directory
A tag handler has access to some properties that are set by the JSP container
using setter methods. This includes the pageContext and parent
objects. The tag handler also has access to server-side objects and enclosing
actions. If the tag is nested, the parent handler of the enclosing tag can be
accessed by using either:
TagSupport.getParent()
TagSupport.findAncestorWithClass(from, class)
The parent handler's statically and dynamically created objects can be obtained
once the parent object is retrieved.
|