Home arrow Java arrow Page 4 - JSP Custom Tags: Bringing Components to the Web
JAVA

JSP Custom Tags: Bringing Components to the Web


This article explains what a JSP custom tag is, how to create one, and why you should use them in your web development projects.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 13
June 13, 2006
TABLE OF CONTENTS:
  1. · JSP Custom Tags: Bringing Components to the Web
  2. · Creating a Custom Tag, Step By Step
  3. · Creating a Custom Tag continued
  4. · Custom Tags in the Real World

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
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...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials