The classes and interfaces used to implement classic tag handlers are contained in the javax.servlet.jsp.tagext package. Classic tag handlers implement either the Tag, the 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. 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.
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.
The JSP page’s servlet invokes the setPageContext, setParent, and attribute-setting methods before calling doStartTag. The JSP page’s servlet also guarantees that release will be invoked on the tag handler before the end of the page.
Here is a typical tag handler method invocation sequence:
ATag t = new ATag(); t.setPageContext(...); t.setParent(...); t.setAttribute1(value1); t.setAttribute2(value2); t.doStartTag(); t.doEndTag(); t.release();
The BodyTag interface extends Tag by defining additional methods that let a tag handler access its body. The interface provides three new methods:
setBodyContent: Creates body content and adds to the tag handler
doInitBody: Called before evaluation of the tag body
doAfterBody: Called after evaluation of the tag body
A typical invocation sequence is as follows:
t.doStartTag(); out = pageContext.pushBody(); t.setBodyContent(out); // perform any initialization needed after body content is set t.doInitBody(); t.doAfterBody(); // while doAfterBody returns EVAL_BODY_AGAIN we // iterate body evaluation ... t.doAfterBody(); t.doEndTag(); out = pageContext.popBody(); t.release();