Java
  Home arrow Java arrow Page 2 - JSP Custom Tags: Bringing Components to th...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
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

JSP Custom Tags: Bringing Components to the Web
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2006-06-13

    Table of Contents:
  • JSP Custom Tags: Bringing Components to the Web
  • Creating a Custom Tag, Step By Step
  • Creating a Custom Tag continued
  • Custom Tags in the Real World

  • 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


    JSP Custom Tags: Bringing Components to the Web - Creating a Custom Tag, Step By Step


    (Page 2 of 4 )

    There are two main steps in creating a custom tag: creating the tag handler, and creating the TLD file. 

    As discussed in the previous section, a tag handler is nothing but a Java class and the TLD file is a mapping document written in XML format.

    Creating the Tag Handler

    A tag is defined in a tag handler. It extends either the TagSupport or BodySupport class. Both of these provide default implementations for the following methods:

     

    If the tag handler has:

    You need to implement the following methods:

    no attributes and no body attributes

    doStartTag, doEndTag, release doStartTag, doEndTag, set/getAttribute (all the attributes) 

    a body with no interaction

    doStartTag, doEndTag, release

    a body with interaction

    doStartTag, doEndTag, release, doInitBody, doAfterBody

       So if a Simple Tag has to be created, the class template would look like this:   

    package someorg.examples
    import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;
    /**
     * This is a simple tag example to show how content is
    added to the
     * output stream when a tag is encountered in a JSP
    page. 
     */
    public class Hello extends TagSupport {
                //define variables for attributes
        /**
          * Getter/Setter for the attribute name as defined
    in the tld file 
          * for this tag

          */
    /**
    * doStartTag is called by the JSP container when the tag
    is encountered
    */
        public int doStartTag() {
                  try {
            JspWriter out = pageContext.getOut();
            //Content to be output by the tag        
                  // Must return SKIP_BODY because if body
    content execution is not supported for this 
                  // tag.
                  return SKIP_BODY;
        }
    /**
     * doEndTag is called by the JSP container when the tag
    is closed
     */
                public int doEndTag(){
                   try {
                JspWriter out = pageContext.getOut
    ();
                      

                    //
                   } catch (Exception ex){
                            throw new Error("All is not well
    in the world.");
                   }
                }
    }

    From the above template, the following points are clear:

    1. The tag handler must extend the TagSupport class if the basic tag is to be created.
    2. The variables that will act as the holders of attribute values have to be declared. For example, if the tag is <hello /> and contains an attribute called "name" i.e.<hello name="blah"/>, then the handler should have a variable called name.
    3. Getters and setters must be defined for the attribute variable. 
    4. The logic for the content  to be produced is implemented in doStartTag() and doEndTag() methods. The logic in doStartTag() is executed when the start of a tag (for example <hello>) is encountered. And the logic in doEndTag() is executed when a tag is closed (for example </hello>). 
    5. If the contents of the body must be executed, then the return of the doStartTag() must be BODY_EVAL, and if not, then BODY_SKIP.
    6. The release() method need not be overridden because its default implementation is sufficient for most purposes.

    That is how a tag handler is written. The next step is to map all the above information into a TLD file so that the JSP container can validate and call the appropriate methods and handlers if more than one handler is present.

    More Java Articles
    More By A.P.Rajshekhar


       · HiIn this article I have discussed the steps to create JSP Custom Tags. Hope it...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT