Home arrow Java & J2EE arrow Page 7 - Overview of Java Web Technologies, Part 2

Developing and Using Custom Tag Libraries - Java

Today, Budi walks us through a refresher and brief overview of server JSP programming. Today's portion covers JavaServer Pages (JSP), with a thorough overview of JavaBeans and Tags. This excerpt comes from chapter one of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).

  1. Overview of Java Web Technologies, Part 2
  2. Sun's Solution
  3. JSP and JavaBeans
  4. Calling a Bean from a JSP Page
  5. Accessing Bean Properties
  6. Custom Tags
  7. Developing and Using Custom Tag Libraries
  8. Writing a Tag Handler
  9. Writing and Using Tags
  10. Model 2 Architecture
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 9
March 03, 2004

print this article



Developing a custom tag library consists of three main steps:

  • Write a tag handler. A tag handler is a Java class that contains the logic for your custom tag. Every time a JSP page encounters a custom tag, the JSP container finds the associated tag handler of the custom tag and executes it. Listing 10 shows an example of a tag handler called MyTagHandler.

  • Write a tag library descriptor (TLD). A TLD is an XML file that defines a tag library and its tags. Listing 11 shows an example of a TLD file called myTaglib.tld.

  • Edit your Web applicationís deployment descriptor (web.xml file). To use custom tags, you must specify a taglib element in the deployment descriptor. Listing 12 shows a deployment descriptor with this element.

  • Write a JSP page that includes the taglib directive. To use a custom tag in a JSP page, you need to use the taglib directive to identify the TLD and tag. Listing 13 shows an example of a .jsp that uses a custom tag.

    Before we go into the details of each of these steps, letís use the examples in Listings 10 through 13 to demonstrate how custom tags work. First, write the tag handler in Listing 10 and compile it to obtain the MySimpleTag.class file. Next, write the TLD file in Listing 11. Then edit the applicationís deployment descriptor as shown in Listing 12. Finally, create the customTagExample.jsp file in Listing 13 to use the custom tag.

Listing 10 The Tag Handler: MyTagHandler.java

package ch01
import javax
import javax
import javax
public class 
MyTagHandler extends TagSupport 
public int doEndTag
() throws JspException 
JspWriter out 

.print("This is the output from the custom tag."); 

(Exception e) { 

return super

Listing 11 The Tag Library Descriptor: myTaglib.tld

<?xml version="1.0" encoding="ISO-8859-1" ? > 
<!DOCTYPE taglib 
"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 

Listing 12 The Deployment Descriptor (web.xml)

<?xml version="1.0" encoding="ISO-8859-1"? > 
<!DOCTYPE web-app 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
<display-name>Custom Tag</display-name

<STRONG>Listing 13</STRONG> <EM>The JSP PagecustomTagExample.jsp</EM>
<%@ taglib uri="/myTLD" prefix="c"%> 
<title>Using custom tags</title

NOTE: Tomcat reports an error if your tag handler belongs to the default package.

To compile the MyTagHandler.java, you need the servlet-api.jar file in your class path. In Tomcat 5, the servlet-api.jar can be found in the common/lib directory under %CATALINA_HOME%. If you copy the servlet-api.jar file to the directory where subdirectory ch01 resides, here is the command to compile the source code:

javac -classpath ./servlet-api.jar ch01/MyTagHandler.java 

The directory structure for deployment is shown in Figure 2. The application name is JSFCh01. Now, start Tomcat and direct your browser to the following URL:


You will see the result shown in Figure 3.

When the JSP container encounters the taglib element in the JSP page, it looks up the deployment descriptor for the location of the taglib where the URI is /myTLD and gets the path to the TLD file. This path is stored in memory for later use. When the custom tag


Figure 2
The directory structure for the application that uses the custom tag


Figure 3 Output from the custom tag

<myTag> is encountered, the JSP container reads the TLD file and finds the fully qualified name of the tag handler. The JSP container then loads the tag handler and processes it.

Figure 4 illustrates this process.


Figure 4 The relationship between the deployment descriptor,
the JSP page, and the TLD file.

The following sections discuss the steps for developing and using a custom tag library in detail.

Buy this book now!Remember: This is part two of the first chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for more chapters of developer books from McGraw-Hill/Osborne.
Buy this book!

>>> More Java & J2EE Articles          >>> More By McGraw-Hill/Osborne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: