JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
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.
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) 2002 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.
herong> javac -cp \local\tomcat\lib\jsp-api.jar herong\HelloTag.java
3. Installing the compiled tag class to the .\WEB-INF\classes directory on the Tomcat server.
herong> mkdir \local\tomcat\webapps\ROOT\WEB-INF\classes herong> xcopy .\herong \local\tomcat\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) 2006 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:
herong> mkdir \local\tomcat\webapps\ROOT\WEB-INF\tlds herong> copy HyTaglib.tld \local\tomcat\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.3"> <!-- hello_tag.jspx - Copyright (c) 2006 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.
herong> copy hello_tag.jspx \local\tomcat\webapps\ROOT
8. Restart Tomcat server.
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.
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
Non-ASCII Characters Support in JSP Pages
Overview of JSTL (JSP Standard Tag Libraries)
Creating and Using Custom Tags
►My First Custom Tag - hy:hello
Multiple Tags Working Together
Using Tomcat on CentOS Systems
Connecting to SQL Server from Servlet