Show TOC

Developing Custom TagsLocate this document in the navigation structure

Use

With JSP custom tags, you can extend functions provided by JSP standard actions and JSTL. Using custom tags, you can encapsulate common tasks into pieces of reusable code for your Web applications. To create custom tags, you either implement Java code or JSP syntax. Depending on the application you develop, you can implement tag handlers or tag files. You can implement the same functions with tag handlers and tag files. We recommend that you use tag files for simple tasks and tag handlers for complex tasks. For example, simple tasks may include formatting output, rendering business objects in markup, or creating sub-views of a page, and complex tasks may include inheritance between tags, interaction with other components and databases, and complicated business logic implementation. The following table summarizes the differences between tag handlers and tag files:

Characteristic

Tag Handlers

Tag Files

Syntax

Written in Java. You can choose to implement a classic tag handler or a simple tag handler.

Written in JSP syntax.

Descriptor

Tags added to TLD using <tag> .

Optional. Tag files added to TLD using <tag-file> .

Packaging

Packaged under WEB-INF/classes or in a JAR file under WEB-INF/lib .

Packaged under WEB-INF/tags or META-INF/tags in a JAR file under WEB-INF/lib .

Import

Imported with the URI attribute of the taglib directive.

Imported either with the TAGDIR attribute of the taglib directive or with the URI attribute of the taglib directive.

Use in JSP Page

Used in the same way.

Process
  1. Implement the tag.

    More information:

  2. Create a TLD and add the tags to the TLD.

    More information:

  3. Package the files.

    You package Java tag handlers as ordinary class files in /WEB-INF/classes or in JAR files in /WEB-INF/lib .

    You package tag files either in /WEB-INF or in some directory in it, or in JAR files located in /WEB-INF/lib under /META-INF directory or some subdirectory of it.

    You package tag library descriptors in /WEB-INF (but not in /WEB-INF/classes or /WEB-INF/lib ) or in JAR files in /WEB-INF/lib under /META-INF directory or some subdirectory of it.

  4. Import the TLD in a JSP page.

    You can import a TLD in a JSP page in four different ways:

    • The recommended way is to describe the TLD in the web.xml using the jsp-config element and its taglib subelement. An explicit taglib map between URIs and TLD resource paths can be described. In your JSP, for the value of taglib directory's uri attribute you use the value of the taglib-uri element from the web.xml.

    • By using directly the URI of the TLD. Each TLD can have an optional tag <uri> , which should be unique in the Web application. Its value can be used directly in the taglib directory's uri attribute.

    • In the taglib directory's uri attribute you can put a relative to the application root path to the TLD.

      Sample Code
      <%@ taglib uri="/WEB-INF/my.tld" prefix="x" %>
                              
    • For tag files only, you use the tagdir attribute of the taglib directive. It should point either to "/ WEB-INF/tags " or a subdirectory of it.

  5. Use the tag in a JSP page.