background image

Classic Tag Handler

<< Programming Tags That Accept Scripting Elements | Tags with Bodies >>
<< Programming Tags That Accept Scripting Elements | Tags with Bodies >>

Classic Tag Handler

IterationTag
, or the
BodyTag
interface. Interfaces can be used to take an existing Java object and
make it a tag handler. For newly created classic tag handlers, you can use the
TagSupport
and
BodyTagSupport
classes as base classes. These classes and interfaces are contained in the
javax.servlet.jsp.tagext
package.
Tag handler methods defined by the Tag and BodyTag interfaces are called by the JSP page's
servlet at various points during the evaluation of the tag. When the start element of a custom tag
is encountered, the JSP page's servlet calls methods to initialize the appropriate handler and
then invokes the handler's doStartTag method. When the end element of a custom tag is
encountered, the handler's doEndTag method is invoked for all but simple tags. Additional
methods are invoked in between when a tag handler needs to manipulate the body of the tag.
For further information, see
"Tags with Bodies" on page 280
. To provide a tag handler
implementation, you must implement the methods, summarized in
Table 9­2
, that are invoked
at various stages of processing the tag.
TABLE 9­2
Tag Handler Methods
Tag Type
Interface
Methods
Basic
Tag
doStartTag
, doEndTag
Attributes
Tag
doStartTag
, doEndTag, setAttribute1,...,N, release
Body
Tag
doStartTag
, doEndTag, release
Body, iterative evaluation
IterationTag
doStartTag
, doAfterBody, doEndTag, release
Body, manipulation
BodyTag
doStartTag
, doEndTag, release, doInitBody, doAfterBody
A tag handler has access to an API that allows it to communicate with the JSP page. The entry
points to the API are two objects: the JSP context (
javax.servlet.jsp.JspContext
) for simple tag
handlers and the page context (
javax.servlet.jsp.PageContext
) for classic tag handlers.
JspContext
provides access to implicit objects. PageContext extends JspContext with
HTTP-specific behavior. A tag handler can retrieve all the other implicit objects (request,
session, and application) that are accessible from a JSP page through these objects. In addition,
implicit objects can have named attributes associated with them. Such attributes are accessed
using [set|get]Attribute methods.
If the tag is nested, a tag handler also has access to the handler (called the parent) associated
with the enclosing tag.
How Is a Classic Tag Handler Invoked?
The Tag interface defines the basic protocol between a tag handler and a JSP page's servlet. It
defines the life cycle and the methods to be invoked when the start and end tags are
encountered.
Programming Tags That Accept Scripting Elements
Chapter 9 · Scripting in JSP Pages
279