Java
  Home arrow Java arrow Page 4 - 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 - Custom Tags in the Real World


    (Page 4 of 4 )

    Up until now I have been discussing the theoretical side of JSP. Now let's see the practical implementation. The tag I am going to develop will provide following functions: create a table with the given number of rows and columns as parameters, and create the heading of the created table with the name given as an attribute.

    So here it goes. The first step is to create the tag handler. The handler has getter and setters for the three attributes: number of rows, number of columns and the table header.  The doStartTag() will output the required table format in HTML. It looks like this:

    package someorg.ui
    import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;
    /**
     * This is a simple tag example to show how content is
    generated. 
     */
    public class TableTag extends TagSupport {
             private String name=null;//header of the table
             private String rows=null;//no. of rows to be
    generated
             private String cols=null;// no. of cols to be
    generated

        /**
          * Getter/Setter for the attributes name of header,
    rows and cols 
          * as defined  
          * in the tld file 
          * for this tag
          */
    public void setName(String value){
        name = value;
    }

    public String getName(){
              return(name);
           }

    public void setRows(String value){
        rows = value;
    }

    public String getRows(){
              return(rows);
           }

    public void setcols(String value){
        cols = value;
    }

    public String getName(){
              return(cols);
           }

    /**
    * doStartTag is called by the JSP container when the tag
    is  
    * encountered. It would output the required HTML
    */
        public int doStartTag() {
              try {
            JspWriter out = pageContext.getOut();
            out.println("<table border="1">");
            out.println("<tr><td colspan=" 
                        "+Integer.parseInt(cols)
    +"">"+name+"</td></tr>");

              for(int i=0;i< Integer.parseInt(rows);i++)
                 {
                        out.println("<tr>");
                        for(int j=0;j< Integer.parseInt
    (cols);j++)
                        {
                           out.println("<td>"+j+"</td>");//for
    now no. cols
                        }
                       out.println("</tr>"); 

                 }         

              } catch (Exception ex) {
                throw new Error("All is not well in the
    world.");
              }
              // Must return SKIP_BODY because we are not
    supporting a body 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();
                   out.println("</table>");
               } catch (Exception ex){
                    throw new Error("All is not well in the
    world.");
               }
            }

    Next is the TLD file. The values are according to the description given in the previous section. All the attributes are mandatory. There is no need for a runtime expression value. The name of the tag is same as the handler. So here is the TLD file:

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
    "http://java.sun.com/j2ee/dtds/web-
    jsptaglibrary_1_1.dtd">
    <taglib>
            <tlibversion>1.0</tlibversion>
            <jspversion>1.1</jspversion>
            <shortname>TableTags</shortname>
            <info>Table Tag library</info>
      <!-A Simple tag -->
      <tag>
        <name>Table</name>
        <tagclass>someorg.ui.TableTag </tagclass>
      <!--Body content can have a value of 
             empty: no body 
          JSP: body that is evaluated by container, then
    possibly processed by the tag
          tagdependent: body is only processed by tag; JSP in
    body is not evaluated.
       -->
        <bodycontent>empty</bodycontent>
        <info>
            This is a simple Table tag.
        </info>
      <!-- mandatory attributes  -->
      <!- personalized name -->
      <attribute>
          <name>name</name>
          <required>true</required>
          <rtexpvalue>false</rtexpvalue>
      </attribute>
    <attribute>
          <name>rows</name>
          <required>true</required>
          <rtexpvalue>false</rtexpvalue>
      </attribute>
    <attribute>
          <name>cols</name>
          <required>true</required>
          <rtexpvalue>false</rtexpvalue>
      </attribute>

    </tag>
    </taglib>

    That completes the real world example. However, this discussion leaves many things unexplained, such as life cycle of a tag, using JSP body content, simple body content, and so forth. That will be the focus of my next discussion. Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 5 hosted by Hostway
    Stay green...Green IT