Datasheet

Patrick c01.tex V3 - 09/18/2009 12:15pm Page 20
Chapter 1: Building Web Applications in WebLogic
Custom Tag Key Concepts
Custom tags require a minimum of three components:
The Tag Handler Class is a Java class implementing either the
javax.servlet.jsp.tagext.Tag
or
BodyTag
interfaces. The tag handler class defines the behavior of the tag when invoked in the
JSP page by providing set methods for attributes and implementations for key methods such as
doStartTag()
and
doEndTag()
.
The Tag Library Descriptor (TLD) file contains XML elements that map the tag name to the tag
handler class and provide additional information about the tag. This file defines whether the tag
contains and manipulates JSP body content, whether it uses a tag extra information class, and the
name of the library containing this tag.
JSP Pages contain
<%@ taglib
...
%>
declarations for the tag library and individual tag elements
in the page itself to invoke the methods contained in the tag handler class.
Custom tags may also define a Tag Extra Information (TEI) class, extending
javax.servlet.jsp.
tagext.TagExtraInfo
, that defines the tag interface in detail and provides the names and types of
scriptlet variables introduced by the tag. During page generation, the JSP engine uses the TEI class to
validate the tags embedded on the page and include the correct Java code in the generated servlet to
introduce variables defined by the custom tag.
Custom Tags are Different from Tag Files
Don’t confuse custom tags with the new tag files functionality added in the JSP 2.0 specification: They are
quite different in both intent and implementation.
As stated above, custom tags provide a powerful mechanism for extending the basic JSP tag syntax to
include custom developed tags for interacting with Java components. They efficiently and safely replace
the use of scriptlet code required to call methods on these Java components.
Tag files, on the other hand, provide a new way to include shared pieces of JSP-generated output within
a main page. They are essentially an alternative to the use of
<jsp:include>
actions or
<%@ include
file="..." %>
directives, and provide no direct access to utility Java classes or other Java components.
Shared JSP pages and page fragments previously located with other JSP content are renamed to end with
a
.tag
suffix and moved to a special
tags
directory below
WEB-INF
. These shared pages and fragments
are then included in the main page content using new custom tags.
Apart from simplifying the passing of parameters to a shared JSP page, and providing the ability for the
called tag file to control when the body of the calling tag is evaluated and inserted in the output, it is
difficult to find any advantage that would pay for the complexity tag files add to an application. If you
find the older, simpler methods for sharing JSP pages and fragments unable to accommodate your needs,
consider using the new tag files mechanism, but don’t convert simple JSP pages and fragments to tag
files without a good reason.
We will not be covering the creation or use of tag files in this book, nor will we be using them in the
example programs.
20