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,, which extends the TagSupport class provided in the Java EE package:

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

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

   -classpath \local\apache-tomcat-7.0.32\lib\jsp-api.jar

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:\>copy HelloTag.class 

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"?>
 "-//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> 

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 

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="" 
   xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="2.1"> 
<!-- hello_tag.jspx
 - Copyright (c) 2012,, All Rights Reserved.
< contentType="text/html"/>

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


 PDF Printing Version