JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

JSP Tag Attribute Handling

Part:   1  2  3  4 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

Tag Attribute Setter Method

The JSP tag extension facility maps each attribute encountered in a custom tag to an property of the same name of the tag object. So if you want to use an attribute in a custom tag, you must define a property in the tag class with the same name as the attribute. Here is the steps you need to follow to add an attribute to a custom tag:

1. Define a property in the tag class with the same name as the attribute name by adding a setter method and a getter method. This is the same way as defining a property in a JavaBean class.

2. Add an "attribute" element inside the "tag" element in your TLD file with the following syntax:

<tag>
 <name>tag_name</name>
 <tag-class>class_full_name</tag-class>
 <body-content>empty | jsp</body-content>
 <attribute>
  <name>attribute_name</name>
  <required>true | false</required>
 </attribute>
</tag>

3. Add the attribute to the custom tag in your JSP page. Then the tag is processed, the setter method of the tag object will be called to pass the attribute value into the tag object.

Tag Attribute Setter Method Example - EchoTag.java

To show you how to use attributes in a custom tag, I wrote the following example tag, EchoTag.java. It does nothing but takes the value of the "message" attribute, and echoes back to the page output with characters reversed.

/**
 * EchoTag.java
 * Copyright (c) 2003 by Dr. Herong Yang. All rights reserved.
 */
package herong;
import java.io.*;
import javax.servlet.jsp.tagext.*;
public class EchoTag extends TagSupport {
   private String message = null;
   public void setMessage(String m) {
      message = m;
   }
   public int doStartTag() {
      try {
         if (message!=null) {
            char[] a = message.toCharArray();
            int n = a.length;
            for (int i=0; i<n/2; i++) {
               char t = a[i];
               a[i] = a[n-1-i];
               a[n-i-1] = t;
            }
            pageContext.getOut().print(a);
            pageContext.getOut().println("<br/>");
         }
      } catch (IOException e) {
         System.err.println(e.toString());
      }
      return SKIP_BODY;
   }
}

Here is the TLD file:

<?xml version="1.0"?>
<!DOCTYPE taglib PUBLIC 
 "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 
 "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_2.dtd"> 
<!-- HyTaglib.tld
     Copyright (c) 2003 by Dr. Herong Yang
-->
<taglib>
<tlib-version>1</tlib-version>
<jsp-version>1.2</jsp-version> 
<short-name>Herong's Tag Library</short-name> 
<tag>
 <name>echo</name>
 <tag-class>herong.EchoTag</tag-class>
 <body-content>empty</body-content>
 <attribute>
  <name>message</name>
  <required>false</required>
 </attribute>
</tag>
</taglib>

Here is a test page, EchoTagTest.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" 
   xmlns:c="http://java.sun.com/jstl/core"
   xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="1.2"> 
<!-- EchoTagTest.jsp
     Copyright (c) 2003 by Dr. Herong Yang
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<hy:echo message="Fish, I love you and respect you very much."/>
</body></html>
</jsp:root>

You can guess what you will be getting when you access this page.

(Continued on next part...)

Part:   1  2  3  4 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - JSP Tag Attribute Handling