Java
  Home arrow Java arrow Page 7 - Overview of Java Web Technologies, Par...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Overview of Java Web Technologies, Part 2
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2004-03-03

    Table of Contents:
  • Overview of Java Web Technologies, Part 2
  • Sun's Solution
  • JSP and JavaBeans
  • Calling a Bean from a JSP Page
  • Accessing Bean Properties
  • Custom Tags
  • Developing and Using Custom Tag Libraries
  • Writing a Tag Handler
  • Writing and Using Tags
  • Model 2 Architecture

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Overview of Java Web Technologies, Part 2 - Developing and Using Custom Tag Libraries


    (Page 7 of 10 )

    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 Articles
    More By McGraw-Hill/Osborne


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup

    BlackBerry VTS




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway