Home > Articles > Programming > Java

JSP Tag Libraries: The Basics

  • Print
  • + Share This
This chapter describes how to create and use custom tags utilizing the new SimpleTag API, which was introduced in version 2.4 of the servlet specification. As its name suggests, SimpleTag API is very easy to use in comparison to its predecessor, now known as the classic tag API.
This chapter is from the book

Topics in This Chapter

  • Identifying tag library components
  • Creating simple custom tags
  • Handling attributes in custom tags
  • Outputting tag bodies
  • Creating JSP-based custom tags with tag files

As discussed in Volume 1 (Section 11.2) of Core Servlets and JavaServer Pages, you have many options when it comes to generating dynamic content inside the JSP page. These options are as follows:

  • Scripting elements calling servlet code directly
  • Scripting elements calling servlet code indirectly (by means of utility classes)
  • Beans
  • Servlet/JSP combo (MVC)
  • MVC with JSP expression language
  • Custom tags

The options at the top of the list are much simpler to use and are just as legitimate as the options at the bottom of the list. However, industry has adopted a best practice to avoid placing Java code inside the JSP page. This best practice stems from it being much harder to debug and maintain Java code inside the JSP page. In addition, JSP pages should concentrate only on the presentation logic. Introducing Java code into the JSP page tends to divert its purpose and, inevitably, business logic starts to creep in. To enforce this best practice, version 2.4 of the servlet specification went so far as to provide a way to disable any type of JSP scripting for a group of JSP pages. We discuss how to disable scripting in Section 2.14 (Configuring JSP Pages).

That said, there are cases where the presentation logic itself is quite complex and using the non-Java code options in the JSP page to express that logic becomes either too clunky and unreadable or, sometimes, just impossible to achieve. This is where logic through the familiar HTML-like structures.

This chapter discusses how to create and use custom tags utilizing the new SimpleTag API, which was introduced in version 2.4 of the servlet specification. As its name suggests, SimpleTag API is very easy to use in comparison to its predecessor, now known as the classic tag API.

Although the SimpleTag API completely replaces the classic tag API, you should keep in mind that it works only in containers compliant with servlet specification 2.4 and above. Because there are still a lot of applications running on servlet 2.3-compliant containers, you should consider avoiding the SimpleTag API if you are not sure what type of container your code will end up on.

7.1 Tag Library Components

To use custom JSP tags, you need to define three separate components:

  • The tag handler class that defines the tag's behavior
  • The TLD file that maps the XML element names to the tag implementations
  • The JSP file that uses the tag library

The rest of this section gives an overview of each of these components, and the following sections give details on how to build these components for various styles of tags. Most people find that the first tag they write is the hardest—the difficulty being in knowing where each component should go, not in writing the components. So, we suggest that you start by just downloading the simplest of the examples of this chapter from http://volume2.coreservlets.com/ and getting those examples to work on your machine. After that, you can move on and try creating some of your own tags.

The Tag Handler Class

When defining a new tag, your first task is to define a Java class that tells the system what to do when it sees the tag. This class must implement the SimpleTag interface. In practice, you extend SimpleTagSupport, which implements the SimpleTag interface and supplies standard implementations for some of its methods. Both the SimpleTag interface and the SimpleTagSupport class reside in the javax.servlet.jsp.tagext package.

The very first action the container takes after loading the tag handler class is instantiating it with its no-arg constructor. This means that every tag handler must have a no-arg constructor or its instantiation will fail. Remember that the Java compiler provides one for you automatically unless you define a constructor with arguments. In that case, be sure to define a no-arg constructor yourself.

The code that does the actual work of the tag goes inside the doTag method. Usually, this code outputs content to the JSP page by invoking the print method of the JspWriter class. To obtain an instance of the JstWriter class you call getJspContext().getOut() inside the doTag method. The doTag method is called at request time. It's important to note that, unlike the classic tag model, the SimpleTag model never reuses tag handler instances. In fact, a new instance of the tag handler class is created for every tag occurrence on the page. This alleviates worries about race conditions and cached values even if you use instance variables in the tag handler class.

You place the compiled tag handler in the same location you would place a regular servlet, inside the WEB-INF/classes directory, keeping the package structure intact. For example, if your tag handler class belongs to the mytags package and its class name is MyTag, you would place the MyTag.class file inside the WEB-INF/classes/mytags/ directory.

Listing 7.1 shows an example of a tag handler class.

Listing 7.1. Example Tag Handler Class

package somepackage;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;

public class ExampleTag extends SimpleTagSupport {
  public void doTag() throws JspException, IOException {
    JspWriter out = getJspContext().getOut();
    out.print("<b>Hello World!</b>");

The Tag Library Descriptor File

Once you have defined a tag handler, your next task is to identify this class to the server and to associate it with a particular XML tag name. This task is accomplished by means of a TLD file in XML format. This file contains some fixed information (e.g., XML Schema instance declaration), an arbitrary short name for your library, a short description, and a series of tag descriptions. Listing 7.2 shows an example TLD file.

Listing 7.2. Example Tag Library Descriptor File

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    <description>Example tag</description>

We describe the details of the contents of the TLD file in later sections. For now, just note that the tag element through the following subelements in their required order defines the custom tag.

  • description. This optional element allows the tag developer to document the purpose of the custom tag.
  • name. This required element defines the name of the tag as it will be referred to by the JSP page (really tag suffix, as will be seen shortly).
  • tag-class. This required element identifies the fully qualified name of the implementing tag handler class.
  • body-content. This required element tells the container how to treat the content between the beginning and ending occurrence of the tag, if any. The value that appears here can be either empty, scriptless, tagdependent, or JSP.

    The value of empty means that no content is allowed to appear in the body of the tag. This would mean that the declared tag can only appear in the form:




    (without any spaces between the opening and closing tags). Placing any content inside the tag body would generate a page translation error.

The value of scriptless means that the tag body is allowed to have JSP content as long as it doesn't contain any scripting elements like <% ... %> or <%= ... %>. If present, the body of the tag would be processed just like any other JSP content.

The value of tagdependent means that the tag is allowed to have any type of content as its body. However, this content is not processed at all and completely ignored. It is up to the developer of the tag handler to get access to that content and do something with it. For example, if you wanted to develop a tag that would allow the JSP page developer to execute an SQL statement, providing the SQL in the body of the tag, you would use tagdependent as the value of the body-content element.

Finally, the value of JSP is provided for backward compatibility with the classic custom tag model. It is not a legal value when used with the SimpleTag API.

Note that there is no legal way of allowing any scripting elements to appear as the tag body under the new SimpleTag API model.

The TLD file must be placed inside the WEB-INF directory or any subdirectory thereof.

We suggest that you don't try to retype the TLD every time you start a new tag library, but start with a template. You can download such a template from http://volume2.coreservlets.com/.

The JSP File

Once you have a tag handler implementation and a TLD, you are ready to write a JSP file that makes use of the tag. Listing 7.3 gives an example. Somewhere in the JSP page you need to place the taglib directive. This directive has the following form:

<%@ taglib uri="..." prefix="..." %>

The required uri attribute can be either an absolute or relative URL referring to a TLD file like the one shown in Listing 7.2. For now, we will use a simple URL relative to the Web application's root directory. This makes it easy to refer to the same TLD file from multiple JSP pages in different directories. Remember that the TLD file must be placed somewhere inside the WEB-INF directory. Because this URL will be resolved on the server and not the client, it is allowed to refer to the WEB-INF directory, which is always protected from direct client access.

The required prefix attribute specifies a prefix to use in front of any tag name defined in the TLD of this taglib declaration. For example, if the TLD file defines a tag named tag1 and the prefix attribute has a value of test, the JSP page would need to refer to the tag as test:tag1. This tag could be used in either of the following two ways, depending on whether it is defined to be a container that makes use of the tag body:

<test:tag1>Arbitrary JSP</test:tag1>

or just

<test:tag1 />

Listing 7.3. Example JSP File

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<TITLE>Example JSP page</TITLE>
<%@ taglib uri="/WEB-INF/tlds/example.tld"
             prefix="test" %>
  • + Share This
  • 🔖 Save To Your Account