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).

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
.servlet.jsp.tagext.TagSupport
import javax
.servlet.jsp.JspException
import javax
.servlet.jsp.JspWriter;
public class 
MyTagHandler extends TagSupport 
public int doEndTag
() throws JspException 
JspWriter out 
pageContext.getOut(); 
try 

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

catch 
(Exception e) { 
System
.out.println(e.toString()); 

return super
.doEndTag(); 



Listing 11 The Tag Library Descriptor: myTaglib.tld

 
<?xml version="1.0" encoding="ISO-8859-1" ? > 
<!DOCTYPE taglib 
PUBLIC 
"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"
<taglib
<tlib-version>1.0</tlib-version
<jsp-version>1.2</jsp-version
<short-name></short-name
<tag
<name>myTag</name
<tag-class>ch01.MyTagHandler</tag-class> 
</tag
</taglib

Listing 12 The Deployment Descriptor (web.xml)

 
<?xml version="1.0" encoding="ISO-8859-1"? > 
<!DOCTYPE web-app 
PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd"
<web-app
<display-name>Custom Tag</display-name
<taglib
<taglib-uri>/myTLD</taglib-uri
<taglib-location>/WEB-INF/myTaglib.tld</taglib-location
</taglib
</web-app>

<STRONG>Listing 13</STRONG> <EM>The JSP PagecustomTagExample.jsp</EM>
[
code
<%@ taglib uri="/myTLD" prefix="c"%> 
<html
<head
<title>Using custom tags</title
</head
<body
<c:myTag/> 
</body
</html



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:

 
http://localhost:8080/JSFCh01/customTagExample.jsp 

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

directory



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

Tomcat

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.

Java


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
   

JAVA & J2EE ARTICLES

- 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: