Skip Headers
Oracle® Fusion Middleware Programming JSP Tag Extensions for Oracle WebLogic Server
12c Release 1 (12.1.1)

Part Number E24388-01
Go to Documentation Home
Home
Go to Table of Contents
Contents
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
PDF · Mobi · ePub

2 Understanding and Creating Custom JSP Tags

This chapter provides an overview of custom JSP tag functionality, format, and components, as well as procedures for creating and configuring a tag library.

This chapter includes the following sections:

Tag Handler API and Implementation

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 JSP 2.1 Specification at http://java.sun.com/products/jsp/reference/api/index.html.

Documentation for the javax.servlet.jsp.tagext API is available at http://download.oracle.com/docs/cd/E17802_01/products/products/jsp/2.1/docs/jsp-2_1-pfd2/index.html.

Your tag handler must be of one of the following two types:

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 SimpleTagSupport, TagSupport, and BodyTagSupport classes implement the SimpleTag, Tag or 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.

Custom Tag Library

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" %>

For more information, see Chapter 3, "Creating a Tag Library Descriptor."

Custom Tag Format

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 Handling Exceptions within a Tag Body.

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" ... >
  body
</mytaglib:newtag>

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!
  <p>
  <mytaglib:repeater repeat=4>
     <p>Hello World!
  </mytaglib:repeater>
</mytaglib:tagA>

The preceding example uses three custom tags to illustrate the ability to nest tags within a body tag. The tags function like this:

What Can You Do with Custom Tags?

Custom tags can perform the following tasks:

Creating and Configuring a JSP Tag Library: Example Procedures

Perform the following steps to create and use custom JSP tags:

  1. Write a tag handler class. When you use a custom tag in your JSP, this class executes the functionality of the tag. A tag handler class implements one of three interfaces:

    javax.servlet.jsp.tagext.BodyTag
    javax.servlet.jsp.tagext.Tag
    javax.servlet.jsp.tagext.SimpleTag
    

    Your tag handler class is implemented as part of a tag library. For more information, see Chapter 4, "Implementing the Tag Handler."

  2. Reference the tag library in your JSP source using the JSP <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.

  3. Write the tag library descriptor (TLD). The TLD defines the tag library and provides additional information about each tag, such as the name of the tag handler class, attributes, and other information about the tags. For more information, see Chapter 3, "Creating a Tag Library Descriptor."

  4. Reference the TLD in the Web application deployment descriptor (web.xml).

  5. Use your custom tag in your JSP. For more information, see Configuring JSP Tag Libraries.