Value Type Conversion Example -

This section provides a tutorial example on how JSP server determines which setter method to call and how to convert the attribute value to match the setter method parameter.

Here is an example of to show you how JSP tag extension facility is converting the attribute values to the tag's setter methods required data types.

The tag class:

 * Copyright (c) 2012,, All Rights Reserved.
package herong;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class AttValueTag extends TagSupport {
   private long longValue = 0;
   private Long longObject = null;
   private double doubleValue = 0.0;
   private Double doubleObject = null;
   private boolean booleanValue = true;
   private Boolean booleanObject = null;
   private String stringObject = null;
   public void setLongValue(long val) {
      longValue = val;
   public void setLongObject(Long obj) {
      longObject = obj;
   public void setDoubleValue(double val) {
      doubleValue = val;
   public void setDoubleObject(Double obj) {
      doubleObject = obj;
   public void setBooleanValue(boolean val) {
      booleanValue = val;
   public void setBooleanObject(Boolean obj) {
      booleanObject = obj;
   public void setStringObject(String obj) {
      stringObject = obj;
   public int doStartTag() {
      JspWriter out = pageContext.getOut();
      try {
         out.println("longValue = "+longValue+"<br/>");
         out.println("longObject = "+longObject+"<br/>");
         out.println("doubleValue = "+doubleValue+"<br/>");
         out.println("doubleObject = "+doubleObject+"<br/>");
         out.println("booleanValue = "+booleanValue+"<br/>");
         out.println("booleanObject = "+booleanObject+"<br/>");
         out.println("stringObject = "+stringObject+"<br/>");
      } catch (IOException e) {
      return SKIP_BODY;

The TLD file:

<?xml version="1.0"?>
 "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 
<!-- HyTaglib.tld
 - Copyright (c) 2012,, All Rights Reserved.
<short-name>Herong's Tag Library</short-name> 


The JSP file:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="" 
   xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="2.1"> 
<!-- AttValueTagTest.jspx
 - Copyright (c) 2012,, All Rights Reserved.
< contentType="text/html"/>
<hy:attValue longObject="1" longValue="2"
 doubleObject="1.1" doubleValue="2.2"
 booleanObject="true" booleanValue="false" 
 stringObject="Hello world!"/>

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 7 server:

_jspx_th_hy_005fattValue_005f0.setStringObject("Hello world!");
   new java.lang.Boolean(true));
   new java.lang.Double(1.1));
_jspx_th_hy_005fattValue_005f0.setLongObject(new java.lang.Long(1l));

I only copied statements that interact with the tag class out of the Servlet class here. Note that:

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

Custom Tag Attributes

 Tag Attributes Are Tag Object Properties

 Setter Method Example -

 Value Type Conversion

Value Type Conversion Example -

 Accepting EL Expressions

 Accepting EL Expression Example

 Passing Expression as String Example

 Using jsp:attribute Action Elements

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials


 PDF Printing Version