Setter Method Example -

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, It does nothing but takes the value of the "message" attribute, and echoes back to the page output with characters reversed.

 * Copyright (c) 2012,, All Rights Reserved.
package herong;
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;
      } catch (IOException e) {
      return SKIP_BODY;

Here is 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> 

Here is a test page, EchoTagTest.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="" 
   xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="2.1"> 
<!-- EchoTagTest.jspx
 - Copyright (c) 2012,, All Rights Reserved.
< contentType="text/html"/>
<hy:echo message="Fish, I love you and respect you very much."/>

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 -

 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