Custom Tag Attribute Example

Attribute sub tag is used to take expression values at run time. Basicaly three type of file is required for creating and dispaly the custom tag.
  1. Tag handler class : The Tag handler class extends TagSupport class and perform action.
  2. TLD (Tag Library Descriptor) file : A TLD file is an XML document with a .tld extension that describes one or more tag extensions.
  3. JSP file : This file is created to use custom tag which are defined in the TDL file.
Tag Library Descriptor (TLD)
A TLD file is an XML document with a .tld extension that describes one or more tag extensions. A TLD file contains the <attribute> subtag element.
The <attribute> subtag element, defined in the DTD
This element can have the following subelements:
<!Element attribute ( name, required?, rtexprvalue) >
  1. <name>
    The <name> attribute describes the name of attribute, and it will be appear in JSP pages using custom tag.
  2. <required>
    The <required> attribute is optional whether this attribute is required (true) or optional (false). The attribute may have default value false.
  3. <rtexprvalue>
    The <rtexprvalue> attribute is optional, Whether the attribute’s value can be dynamically calculated at runtime by an expression.
Custom Tag Attribute Example using Tomcat6.0
package ns; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class InputNameTag extends TagSupport { private String name; public String getName() { return name; } public void setName(String name) { = name; } public int doStartTag() throws JspTagException { return EVAL_BODY_INCLUDE; } public int doEndTag() throws JspTagException { try { JspWriter out=pageContext.getOut(); out.print("Custom Tag processing Attribute Example"); out.println("User Name :" + name); } catch(Exception e) { } return EVAL_PAGE; } }
<?xml version="1.0" encoding="UTF-8"?> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>hello</short-name> <uri>inputName.tld</uri> <tag> <name>example</name> <tag-class>ns.InputNameTag </tag-class> <body-content>EMPTY</body-content> <attribute> <name>name</name> <required> true </required> </attribute> </tag> </taglib>
<%@ taglib uri="inputName.tld" prefix="mytag" %> <html> <head> <title>Simple custom tag Arribute example </title> </head> <body> Welcome to Attribute processing custom tag example <h1> <mytag:example name="sunil"/> </h1> <br> <h2> <mytag:example name="kumar" /> </h2> </body> </html>
tag attributes
Download this example
1.) Deployed using Tomcat 6.0
2.) Deployed using Eclipse


Latest Tutorial

Most Visited Tutorial