The following sections provide an overview of custom JSP tag functionality, format, and components, as well as procedures for creating and configuring a tag library:
You write a custom JSP tag by writing a Java class called a tag handler.
The JSP 2.1 API defines a set of classes and interfaces that you use to write custom tag handlers, as described in the.
Documentation for the
javax.servlet.jsp.tagext API is available at .
Your tag handler must be of one of the following two types:
javax.servlet.jsp.tagext.Taginterface if you are creating a custom tag that does not need access to its interface. The API also provides a convenience class
TagSupportthat implements the
Taginterface and provides default empty methods for the methods defined in the interface.
javax.servlet.jsp.tagext.BodyTaginterface if your custom tag needs to use a body. The API also provides a convenience class
BodyTagSupportthat implements the
BodyTaginterface and provides default empty methods for the methods defined in the interface. Because
Tagit is a super set of the interface methods.
javax.servlet.jsp.tagext.IterationTaginterface to extend
Tagby defining an additional method
doAfterBody()that controls the reevaluation of the body.
javax.servlet.jsp.tagext.SimpleTag interface if you wish to use a much simpler invocation protocol. The
SimpleTag interface does not extend the
javax.servlet.jsp.tagext.Tag interface as does the
BodyTag interface. Therefore, instead of supporting the doStartTag() and doEndTag() methods, the
SimpleTag interface provides a simple doTag() method, which is called once and only once for each tag invocation.
You write the tag handler class by doing one of the following:
Extending an abstract base class relieves the tag handler class from having to implement all methods in the interfaces and also provides other convenient functionality. The
BodyTagSupport classes implement the
BodyTag interfaces and are included in the API.
You can include one or more custom JSP tags in a tag library. You define a tag library by a tag library descriptor (
.tld) file. The TLD describes the syntax for each tag and ties it to the Java classes that execute its functionality.
JSP tag libraries include one or more custom JSP tags and are defined in a tag library descriptor (
.tld) file. To use a custom tag library from a JSP page, reference its tag library descriptor with a
<%@ taglib %> directive. For example:
<%@ taglib uri="myTLD" prefix="mytaglib" %>
uriattribute to a
urithat is defined in the Web application deployment descriptor
<taglib-uri>element. For example,
myTLDin the above the
taglibdirective would reference its tag library descriptor
(library.tld)in the Web application deployment descriptor like this:
|Note:||There is no need to mention the
prefixattribute assigns a label to the tag library. You use this label to reference its associated tag library when writing your pages using custom JSP tags. For example, if the library (called
mytaglib) from the example above defines a new tag called
newtag, you would use the tag in your JSP page like this:
For more information, see Creating a Tag Library Descriptor.
A custom tag format can be empty, called an empty tag, or can contain a body, called a body tag. Both types of tags can accept a number of attributes that are passed to the Java class that implements the tag. For more details, see.
An empty tag takes the following form:
<mytaglib:newtag attr1="aaa" attr2="bbb" ... />
A body tag takes the following form:
<mytaglib:newtag attr1="aaa" attr2="bbb" ... >
A tag body can include more JSP syntax, and even other custom JSP tags that also have nested bodies. Tags can be nested within each other to any level. For example:
<mytaglib:tagA><h2>This is the body of tagA</h2>
You have seen this text <mytaglib:counter /> times!
The preceding example uses three custom tags to illustrate the ability to nest tags within a body tag. The tags function like this:
<mytaglib:tagA>only sees the HTML output from its evaluated body. That is, the nested JSP tags
<mytaglib:repeater>are first evaluated and their output becomes part of the evaluated body of the
Custom tags can perform the following tasks:
Perform the following steps to create and use custom JSP tags:
<taglib>directive. A tag library is a collection of JSP tags. Include this directive at the top of your JSP source. For more information, see Configuring JSP Tag Libraries.