background image

Tags with Bodies

<< Classic Tag Handler | Tag Handler Manipulates the Body >>
<< Classic Tag Handler | Tag Handler Manipulates the Body >>

Tags with Bodies

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();
Tags with Bodies
A tag handler for a tag with a body is implemented differently depending on whether or not the
tag handler needs to manipulate the body. A tag handler manipulates the body when it reads or
modifies the contents of the body.
Programming Tags That Accept Scripting Elements
The Java EE 5 Tutorial · September 2007
280