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

(Continued from previous part...)

The JSP file:

<?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"> 
<!-- AttValueTagTest.jsp
     Copyright (c) 2003 by Dr. Herong Yang
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<hy:attValue longObject="1" longValue="2"
 doubleObject="1.1" doubleValue="2.2"
 booleanObject="true" booleanValue="false" 
 stringObject="Hello world!"/>
</body></html>
</jsp:root>

The output:

longValue = 2
longObject = 1
doubleValue = 2.2
doubleObject = 1.1
booleanValue = false
booleanObject = true
stringObject = Hello world

The Servlet class produced by Tomcat server:

...
  private boolean _jspx_meth_hy_attValue_0(
     javax.servlet.jsp.PageContext pageContext)
          throws Throwable {
    JspWriter out = pageContext.getOut();
    /* ----  hy:attValue ---- */
    herong.AttValueTag _jspx_th_hy_attValue_0 = (herong.AttValueTag) 
    _jspx_tagPool_hy_attValue_stringObject_longValue_longObject...
    _jspx_th_hy_attValue_0.setPageContext(pageContext);
    _jspx_th_hy_attValue_0.setParent(null);
    _jspx_th_hy_attValue_0.setLongObject(new Long(1l));
    _jspx_th_hy_attValue_0.setLongValue(2l);
    _jspx_th_hy_attValue_0.setDoubleObject(new Double(1.1));
    _jspx_th_hy_attValue_0.setDoubleValue(2.2);
    _jspx_th_hy_attValue_0.setBooleanObject(new Boolean(true));
    _jspx_th_hy_attValue_0.setBooleanValue(false);
    _jspx_th_hy_attValue_0.setStringObject("Hello world!");
    int _jspx_eval_hy_attValue_0 = _jspx_th_hy_attValue_0.doStartTag();
    if (_jspx_th_hy_attValue_0.doEndTag() == 
       javax.servlet.jsp.tagext.Tag.SKIP_PAGE)
      return true;
    _jspx_tagPool_hy_attValue_stringObject_longValue_longObject...
    return false;
  }
...

I only copied the method that interacts with the tag class out of the Servlet class here. Note that:

  • The setter methods for the primitive data types are called with attribute values directly, except that "l" is added to the end of the value, like "1l".
  • The setter methods for the wrapper classes of the primitive data types are called with new objects instantiated with the attribute values.
  • How can Tomcat server figure out the class type of the setter method parameter? Could someone help me here?

Tag Attribute Value Expression

In the previous sections, I only talked about how to define an attribute in a custom tag, how attribute values are passed to the tag class, and how attribute values are converted to correct data type required by the tag class. Now let's look at the possibility of entering expressions as attribute values in a custom tag, similar to JSTL tags.

Based on my readings on the Internet, expression language is not supported directly in custom tags in JSP 1.2. However there are two approaches to use expressions indirectly in custom tags.

1. Using Java expression elements as attribute values. You need to define the attribute in the TLD file with <rtexprvalue>true</rtexprvalue>, then enter the attribute value with the following format:

<hy:tag att="<%=java_expression%>"/>

2. Using JSTL expressions and evaluate them inside tag class. You can enter a JSTL expression in an attribute value, received it by the setter method as a string, then evaluate it to the desire data type using the expression evaluation tool offer by the JSTL.

The first approach requires the JSP page to be written in a non-XML format. And I don't like that format. So I am not going to try that.

The second approach seems to be interesting and powerful. You can use it to pass an object of any class as an attribute value to the tag class. See the example bellow.

(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