My First Custom Tag - hy:hello

This section provides a tutorial example on creating the first custom tag hy:hello that inserts 'Hello world!' from the tag class into the JSP output.

"Hello world!" Custom Tag

Before we go into any technical details, let me use a very simple example to show you those steps of creating and using a custom tag described in the previous section.

In this example, I want to define a tag called <hy:hello/> to produce the "Hello world!" in the calling JSP page.

1. Writing the tag class. Here is my first tag class, HelloTag.java, which extends the TagSupport class provided in the Java EE package:

/**
 * HelloTag.java
 * Copyright (c) 2012, HerongYang.com, All Rights Reserved.
 */
package herong;
import java.io.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag extends TagSupport {
   public int doStartTag() {
      try {
         pageContext.getOut().write("Hello world! - From HelloTag");
      } catch (IOException e) {
         System.err.println(e.toString());
      }
      return SKIP_BODY;
   }
}

2. Compiling the tag class with the help of the jsp-api.jar provided by the Tomcat server.

C:\>\Progra~1\java\jdk1.7.0_07\bin\javac.exe 
   -classpath \local\apache-tomcat-7.0.32\lib\jsp-api.jar 
   HelloTag.java

3. Installing the compiled tag class to the .\WEB-INF\classes directory on the Tomcat server.

C:\>mkdir \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes

C:\>mkdir 
   \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes\herong

C:\>copy HelloTag.class 
   \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes\herong

4. Creating the TLD (Tag Library Descriptor) file, HyTaglib.tld. In this TLD file, only 1 tag is defined, the "hello" tag:

<?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>hello</name>
 <tag-class>herong.HelloTag</tag-class>
 <body-content>empty</body-content>
</tag>
</taglib>

5. Installing the TLD file. Tag library descriptor (TLD) files need to be accessible by the tomcat server in the .\WEB-INF\tlds directory:

C:\>mkdir \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\tlds

C:\>copy HyTaglib.tld 
   \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\tlds

Don't forget to restart the Tomcat server.

6. Writing a JSP page to use the new custom tag. You need to declare a new namespace for your new TLD file, and use the tag as an action element:

<?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"> 
<!-- hello_tag.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<hy:hello/>
</body></html>
</jsp:root>

7. Deploying JSP pages to the Tomcat server.

C:\>copy hello_tag.jspx \local\apache-tomcat-7.0.32\webapps\ROOT

We are ready to visit the JSP page at: http://localhost:8080/hello_tag.jspx. The output on the brower confirms that my custom tag is working!

Hello world! - From HelloTag

If you are changing your tag class after it has been loaded by tomcat, you may need to restart Tomcat, or click "restart" on the "root" application on the Tomcat admin page. "root" application is where I put my JSP pages.

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

 What Is Custom Tag?

 Creating and Using Custom Tags

My First Custom Tag - hy:hello

 How Custom Tag Works

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version