Setter Method Example - EchoTag.java

This section provides a tutorial example on how to define a setter method to support a custom tag attribute.

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) 2012, HerongYang.com, 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) 2012, HerongYang.com, All Rights Reserved.
-->
<taglib>
<tlib-version>1</tlib-version>
<jsp-version>2.1</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:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="2.1"> 
<!-- EchoTagTest.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<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.

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 - EchoTag.java

 Value Type Conversion

 Value Type Conversion Example - AttValueTag.java

 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

 References

 PDF Printing Version