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.1. Creating a Classic Tag Handler

Problem

You want to create a classic JSP 1.2-style tag handler for a custom action.

Solution

Create a Java class that extends one of the Tag support classes in the javax.servlet.jsp.tagext package, such as BodyTagSupport.

Discussion

There are numerous types of custom tags you can create for JSPs, such as actions that ignore their bodies (empty tags), actions that are nested within other custom actions, and custom tags that use their body content. In fact, entire books have been dedicated solely to JSP custom tag development! Instead of being exhaustive in this book, I show how to create a fairly simple classic tag that adds an image logo to a JSP page with a text message. You can then infer details for your own programming tasks from this example.

The sample tag is designed to allow a page designer to specify an logo's image, its width and height, and a text message to sit alongside the image.

Example 22-1 shows the classic tag handler for this custom action. This Java class extends BodyTagSupport , since it uses the tag's nested content for the logo's text message.

Example 22-1. A classic tag handler for inserting an image and markup

package com.jspservletcookbook;    

import javax.servlet.*;
import javax.servlet.http.*;import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; /** This tag generates a thumbnail image using the HTML img tag, next to a text message. The user specifies the content of the message and the Heading level (i.e., <H1>-<H6>) ...

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