Custom Tag Example

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 <taglib> root element.
The root element is <taglib>, defined in the DTD

This element can have the following subelements:

<!Element taglib ( tlib-version, jsp-version?, descrition, short-name, uri?, tag+) >

  1. <tlib-version>
    The tag describes the version number of the tag library.
  2. <jsp-version>
    The <jsp-version> element specifies the JSP version.
  3. <short-name>
    The <short-name> tag gives a short name for the tag library that might be used within a JSP Page Authoring tool.
  4. <uri>
    A URI that uniquely identifies the tag library.
  5. <tag>
    The tag element is subelement of taglib element. This is an important important subelement in a tag library.
  6. <description>
    The <description> tag gives the long description of the tag library.
The <tag> element, defined in the DTD

This element can have the following subelements:

<!Element tag (name, tag-class, tei-class?, body-content, variable*, attribute*, ) >

  1. <name>
    The <name> is the name that will identify this tag (after the tag library prefix).
  2. <tag-class>
    The <tag-class> is a fully qualified name of the tag handler class that implements this tag.
  3. <tei-class>
    The <tei-class> defines extra information about this tag at run time. It is optional.
  4. <body-content>
    It is an optional attributes that specifies the type of body content.
  5. <variable>
    The <variable> sub tags are used to indicate that a tag declares scripting variables available to JSP page useing it.
  6. <attribute>
    The <attribute> sub-tag of a <tag> element describe each attribute accepted (or required) by the tag.
Import and use of Tag extensions in JSP pages
The custom tag must be explicitly imported into JSP pages before they can be used using a taglib directive.
Syntax of import custom tag using taglib directive
<%@ taglib uri="<uri>" prefix="mytag" %>
Syntax of using custom tag in JSP pages
There are two way to use custom tag in jsp page. These are following :
1. When a tag requires body content :
<mytag:tagname att_name=value >
body code
<mytag:tabname> 2. When a tag requires no body content : <mytag:tagname att_name=value />
Custom Tag Example using Tomcat6.0
WelcomeTag.java
package ns; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class WelcomeTag extends TagSupport { public int doStartTag() throws JspTagException { return EVAL_BODY_INCLUDE; } public int doEndTag() throws JspTagException { try { JspWriter out=pageContext.getOut(); out.print("Custom Tag Example"); } catch(Exception e) { } return EVAL_PAGE; } }
welcome.tld
<?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>welcome.tld</uri> <tag> <name>example</name> <tag-class>ns.WelcomeTag </tag-class> <body-content>EMPTY</body-content> </tag> </taglib>
index.jsp
<%@ taglib uri="welcome.tld" prefix="mytag" %> <html> <head> <title>Simple custom tag example </title> </head> <body> Welcome to first simple custom tag example <h1> <mytag:example/> </h1> <br> <h2> <mytag:example/> </h2> </body> </html>
Output
custom tag
Download this example
1.) Deployed using Tomcat 6.0
2.) Deployed using Eclipse
 



 

Latest Tutorial


Most Visited Tutorial