Mega Code Archive

 
Categories / Java / JSP
 

Tag lifecycle with Attribute

<!-- this must be added to the web application's web.xml --> <taglib>   <taglib-uri>/rntsoft</taglib-uri>   <taglib-location>/WEB-INF/rntsoft.tld</taglib-location> </taglib> // create File:rntsoft.tld in the /WEB-INF/ <!DOCTYPE taglib   PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"    "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">     <!-- a tab library descriptor --> <taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">   <tlib-version>1.0</tlib-version>   <jsp-version>1.2</jsp-version>   <short-name>rntsoft Simple Tags</short-name>   <!-- a tag that outputs information when different methods are called -->   <tag>     <name>tagLifecycle</name>     <tag-class>com.rntsoft.TagLifecycle</tag-class>     <body-content>empty</body-content>     <attribute>       <name>attr1</name>     </attribute>     <attribute>       <name>attr2</name>     </attribute>   </tag> </taglib> //compile the following code into WEB-INF\classes\com\rntsoft package com.rntsoft; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.Tag; import javax.servlet.jsp.tagext.TagSupport; /* This tag handler outputs information about when different methods    are called in the Tag interface.  */ public class TagLifecycle extends TagSupport {   // Constructor   public TagLifecycle()   {     System.out.println("TagLifecycle constructor called");   }   // Methods inherited from TagSupport   public void setPageContext(PageContext p)   {     super.setPageContext(p);     System.out.println("setPageContext() called (" + p + ")");   }      public void setParent(Tag t)   {     super.setParent(t);     System.out.println("setParent() called (" + t + ")");   }   public Tag getParent()   {     System.out.println("getParent() called");     return super.getParent();   }      public void release()   {     System.out.println("release() called");   }      // Code to implement the "attr1" attribute   private String attr1;   public String getAttr1()   {     return attr1;   }   public void setAttr1(String s)   {     System.out.println("setAttr1() called with value " + s);     attr1 = s;   }   // Code to implement the "attr2" attribute   private String attr2;   public String getAttr2()   {     return attr2;   }   public void setAttr2(String s)   {     System.out.println("setAttr2() called with value " + s);     attr2 = s;   }   public int doStartTag() throws JspException   {     System.out.println("doStartTag() called");     return SKIP_BODY;   }   public int doEndTag() throws JspException   {     System.out.println("doEndTag() called");     return super.doEndTag();   } } // start comcat and load the following jsp page in browser <%@ taglib uri="/rntsoft" prefix="rntsoft" %> <html>   <head>     <title>The lifecycle of the Tag interface</title>   </head>   <body>     <rntsoft:tagLifecycle attr1="Joe" attr2="Greenhough" />     <rntsoft:tagLifecycle attr1="Rob" attr2="Greenhough" />     Check the Tomcat console for the output   </body> </html>