Professional Java Server Programming J2EE Edition
Chapter 12
Why, besides a clever syntax, might we choose to use tag extensions rather than JSP beans? Are tag
extensions not simply another way of allowing JSP pages to parcel out work to Java classes?
Due to the richer interaction between the hosting JSP page and tag extensions, tag extensions can achieve
directly what beans can only achieve in conjunction with scriptlets. Tag extensions may access the
PageContext, write output to the output writer, redirect the response, and define scripting variables. As an
indication of their power, all the standard JSP actions provided via tags of form <jsp:XXXX> could be
implemented using tag extensions. The behavior of tag handlers is configured by their XML attributes and
their body content (which can be the result of evaluating JSP expressions at runtime).
Typical uses of tag extensions are:
- To conceal the complexity of access to a data source or enterprise object from the page
(possibly, the page author, who may not be experienced with enterprise data)
- To introduce new scripting variables into the page
- To filter or transform tag content, or even interpret it as another language
- To handle iteration without the need for scriptlets
Tag extensions can be used to deliver a range of functionality limited only by developers' imaginations and
sensible programming practice.
Tag extensions differ from beans in that they are common building blocks, not tailored resources for a
particular page or group of pages. Tags receive the attributes that control their behavior from the JSP
pages using them, not the request to a particular JSP (as in the case of request-bean property mappings).
A well-designed tag extension may be used in many JSP pages.
This reusability is particularly important. Since their implementation and interaction with the JSP engine is
well defined in the JSP 1.1 specification, libraries of tag extensions can be developed and distributed, on a
commercial or open source basis. Generic tags can be developed for particular industries or application types.
Tag extensions, although new to JSP, are a very old concept in dynamic page generation. Products such as
Apple's WebObjects and BroadVision have delivered rich functionality through custom tags for years,
although in a proprietary context. This experience in the use of custom tags can be valuable to JSP
developers.
Tag extensions are a particularly welcome addition to the JSP developer's armory because they are easy to
implement. The API surrounding them is relatively simple, and it is possible to use them to achieve results
quickly. This is a reflection of the elegance of the design of the tag extension mechanism; in the true Java
spirit it delivers rich functionality without excessive complexity.
The examples and most of the discussion in this chapter and the next assume that tag extensions will be
used to generate HTML markup. While this is currently their most likely use, tag extensions can be used
to generate any content type supported by JSP.
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.