Professional Java Server Programming J2EE Edition
Chapter 12
- Next, the container calls the tag handler's doStartTag() method.
- The container calls the doEndTag() method.
- The container calls the release() method. This is not equivalent to a finalizer. Tag handlers
differ from page beans in that their lifecycle is entirely independent of that of the JSPs that use
them. Tag handlers must support repeated use before destruction, possibly in a number of
JSPs. The implementation of the release() method must ensure that any state that may cause
conflict in future uses is reset, and that any resources required during the tag's execution are freed.
Lets look at the doStartTag() and doEndTag() methods:
int doStartTag() throws JspException
Called after the tag has been initialized, when the JSP engine encounters the opening of a tag at run time. Its
return value should be one of two constants defined in the Tag interface: EVAL_BODY_INCLUDE, which
instructs the JSP engine to evaluate both the tag's body and any child tags it has, or SKIP_BODY, which
instructs the JSP engine to ignore the body. This can throw a JspException, as will most of the methods in
the tag handler API when an error condition is encountered; how it will be handled will depend on the JSP
page using the tag. Most JSP pages will use an error page, so an exception thrown in a tag will abort the
rendering of the page.
int doEndTag() throws JspException
doEndTag() is called when the JSP engine encounters the closing tag of an element at run time. Its return
value can be EVAL_PAGE or SKIP_PAGE. EVAL_PAGE will cause the JSP engine to evaluate the rest of the
page, SKIP_PAGE to terminate evaluation of the page. The SKIP_PAGE return value should be used only
with very good reason; using tag handlers to terminate page evaluation is even worse than sprinkling random
return statements in Java code, and may be confusing to the reader. A legitimate use might be to terminate
page output if it is established that the user has insufficient privileges to view the whole of the page.
There are also a number of methods that relate to tag nesting, initialization, and reuse:
Tag getParent()
void setParent()
The specification also requires methods to expose the parent property. A tag's parent is the tag that
directly encloses it in a JSP, or null if there is no enclosing tag. Tag implementations can query their
parent at runtime, to obtain context information:
void setPageContext (PageContext pc)
setPageContext() is an initialization method, making the PageContext of the JSP available to the tag.
void release()
release() is a call to the tag handler to release any resources. Examples of this may be closing JDBC
connections or open sockets that the handler requires for its function and to clear any state associated with it.
The second is an often overlooked task in this context.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.