O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

22.8. Creating a Simple Tag Handler

Problem

You want to create a JSP 2.0 simple tag handler.

Solution

Create a Java class that either implements the SimpleTag interface or extends the SimpleTagSupport class.

Discussion

In an effort to simplify custom tag development, the JSP 2.0 specification added the javax.servlet.jsp.tagext.SimpleTag interface and the SimpleTagSupport class. The SimpleTagSupport class is designed to be the base class for tag handlers that implement SimpleTag. These tag handlers have to implement just one method, doTag( ) .

Tip

The JSP 2.0 specification states that vendors should not cache simple tag handlers, so developers do not have to worry about the reuse of tag handler objects and releasing object state in their code.

Example 22-6 mimics the logo tag handler created in earlier recipes, but uses the SimpleTagSupport class from the JSP 2.0 API instead.

Example 22-6. A simple tag handler displaying a logo

package com.jspservletcookbook;    

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.http.*;import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/** This tag generates a thumbnail image using an HTML img tag, aligned next to a text 
message. The user specifies the content of the message and the Heading level (i.e., <H1>-
<H6>) */

public class SimpleLogoTag extends SimpleTagSupport{

    private String heading = null;
    private String image =null;
    private String width =null;
    private String height =null;

  public void doTag( ) throws JspException, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required