Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

7.6 Example: Heading Tag

Listing 7.11 shows HeadingTag.java, which defines a tag for a heading element that is more flexible than the standard HTML H1 through H6 elements. (Yes, we know that the entire problem could be solved more elegantly with Cascading Style Sheets [CSS] and without the use of a custom tag, but this is for demonstration purposes only, so work with us.) This new element allows a precise font size, a list of preferred font names (the first entry that is available on the client system will be used), a foreground color, a background color, a border, and an alignment (LEFT, CENTER, RIGHT). Only the alignment capability is available with the H1 through H6 elements. The heading is implemented through use of a one-cell table enclosing a SPAN element that has embedded stylesheet attributes.

The doTag method first generates the <TABLE> and <SPAN> start tags, then invokes getJspBody().invoke(null) to instruct the system to include the tag body, and then generates the </SPAN> and </TABLE> tags. We use various setAttributeName methods to handle the attributes like bgColor and fontSize.

Listing 7.12 shows the excerpt from the csajsp-taglib.tld file that defines the heading tag. Listing 7.13 shows heading-1.jsp, which uses the heading tag. Figure 7-3 shows the resulting JSP page.

Listing 7.11. HeadingTag.java

package coreservlets.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;

/** Heading tag allows the JSP developer to create
 *  a heading and specify alignment, background color,
 *  foreground color, font, etc. for that heading.
public class HeadingTag extends SimpleTagSupport {
  private String align;
  private String bgColor;
  private String border;
  private String fgColor;
  private String font;
  private String size;

  public void setAlign(String align) {
    this.align = align;
  public void setBgColor(String bgColor) {
    this.bgColor = bgColor;
  public void setBorder(String border) {
    this.border = border;
  public void setFgColor(String fgColor) {
    this.fgColor = fgColor;
  public void setFont(String font) {
    this.font = font;
  public void setSize(String size) {
    this.size = size;

  public void doTag() throws JspException, IOException {
     JspWriter out = getJspContext().getOut();
     out.print("<TABLE ALIGN=\"" + align + "\"\n" +
                "       BGCOLOR=\"" + bgColor + "\"\n" +
                "       BORDER=" + border + "\">\n");
     out.print("<SPAN STYLE=\"color: " + fgColor + ";\n" +
                "              font-family: " + font + ";\n" +
                "              font-size: " + size + "px; " +
     // Output content of the body
     out.println("</SPAN></TH></TR></TABLE>" +
                  "<BR CLEAR=\"ALL\"><BR>");

Listing 7.12. Excerpt from csajsp-taglib.tld

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

    <description>Formats enclosed heading</description>

Listing 7.13. heading-1.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ taglib uri="/WEB-INF/tlds/csajsp-taglib.tld"
           prefix="csajsp" %>
<csajsp:heading align="LEFT" bgColor="CYAN"
                  border="10" fgColor="BLACK"
                  font="Arial Black" size="78">
  First Heading

<csajsp:heading align="RIGHT" bgColor="RED"
                  border="1" fgColor="YELLOW"
                  font="Times New Roman" size="50">
  Second Heading

<csajsp:heading align="CENTER" bgColor="#C0C0C0"
                  border="20" fgColor="BLUE"
                  font="Arial Narrow" size="100">
  Third Heading
Figure 7-3

Figure 7-3 Result of heading-1.jsp.

  • + Share This
  • 🔖 Save To Your Account