Enterprise Beans

Contact Us

Developing JSP Custom Tags


Custom tags are distributed in a tag library, which defines a set of related custom tags and contains the objects that implement the tags. The object that implements a custom tag is called a tag handler.

JSP technology defines two types of tag handlers: simple and classic.
Simple tag handlers can be used only for tags that do not use scripting elements in attribute values or the tag body.
Classic tag handlers must be used if scripting elements are required.

 Develop a Tag Handler

To develop a tag handler for an empty tag, you can extend the TagSupport class of the javax.servlet.jsp.tagext package in your tag handler. The following code snippet shows a tag handler, WelcomeTag that extends the TagSupport class to implement a custom tag:

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class WelcomeTag extends TagSupport
  • doStartTag(): This method is called when the container encounters the start tag of a custom tag. You can override this method in your tag handler to implement the functionality of your tag. For an empty custom tag, the doStart() method needs to return the int field SKIP_BODY defined in the Tag interface.
  • doEndTag(): This method is called when the container encounters the end tag of a custom tag. For an empty custom tag, the doEndTag() method needs to return SKIP_PAGE.
 Develop the TLD File

A TLD file defines a custom tag in XML format. A TLD file stored with a .tld extension. If a TLD file defines two custom tags, there will be two <tag> elements within the <taglib> element.
Element Name Description
<tlib-version> Defines the version of the tag library. The element, <tlib-version></tlib-version>, must be declared when you define a new tag library.
<jsp-version> Defines the version of the JSP page that the tag library uses.
<short-name> Defines a short name of the tag library that refers to the tag library.
<description> Describes the tag library, such as the type of tags contained in the tag library.
<uri> Defines a unique id for the tag library.
<tag> Contains elements to define a custom tag

The tag element that appears within the taglib element defines the custom tag. The following table lists different sub-elements of the tag element:
Element Name Description
<name> Defines the name of custom tag. This is a required tag because it must be defined while creating a JSP custom tag.
<tag-class> Defines the tag handler class that provides the functionality of custom tag. It is a required element and you must specify the fully qualified name of the class.
<description> Defines the tag functionality. This is an optional element.
<body-content> Defines the body content enclosed within the opening and closing tag of the custom tag. For empty custom tag, the body of this element is empty.

The following code snippet shows a TLD file that defines an empty tag, Welcome implemented by the WelcomeTag tag handler:
<short-name>Welcome Tag</short-name>
A custom tag to display welcome message
 Include the Tag Library in a JSP Page

You can use the following code snippet to include the tag library defined by Welcome.tld in your JSP page:
<%@ taglib uri="/Welcome.tld" prefix="mytag"%>
Once you have included the tag library, you can use the following code snippet to use the custom tag:
<mytag:Welcome />

 Click for Next Topic