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("<TR><TH>");
     out.print("<SPAN STYLE=\"color: " + fgColor + ";\n" +
                "              font-family: " + font + ";\n" +
                "              font-size: " + size + "px; " +
                "\">\n");
     // Output content of the body
     getJspBody().invoke(null);
     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"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">
  <tlib-version>1.0</tlib-version>
  <short-name>csajsp-taglib</short-name>

<tag>
    <description>Formats enclosed heading</description>
    <name>heading</name>
    <tag-class>coreservlets.tags.HeadingTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
      <name>align</name>
      <required>true</required>
    </attribute>
    <attribute>
      <name>bgColor</name>
      <required>true</required>
    </attribute>
    <attribute>
      <name>border</name>
      <required>true</required>
    </attribute>
    <attribute>
      <name>fgColor</name>
      <required>true</required>
    </attribute>
    <attribute>
      <name>font</name>
      <required>true</required>
    </attribute>
    <attribute>
      <name>size</name>
      <required>true</required>
    </attribute>
  </tag>
</taglib>

Listing 7.13. heading-1.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Headings</TITLE>
<LINK REL=STYLESHEET
      HREF="JSP-Styles.css"
      TYPE="text/css">
</HEAD>
<BODY>
<%@ 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>


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


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

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

  • + Share This
  • 🔖 Save To Your Account