Share on Google+Share on Google+

JSP custom tags (user define tags)

Advertisement
In this section , we will discuss about how to create "custom" tags & use it in JSP programs.

JSP custom tags (user define tags)

In this section , we will discuss about how to create "custom" tags & use  it in JSP programs.

JSP Custom Tags :
  • Provide a mechanism to a Web programmer to reuse and encapsulate complex recurring code in a JSP application.

  •  Provide simplicity and reusability of Java code.

  • Enable you to perform various functions, such as:

  1. Accessing all implicit variables of a JSP page, such as request, response, in, and out.

  2.  Modifying the response generated by a calling JSP page.

  3. Initializing and instantiating a JavaBean component.
Types of Custom Tags

The various types of custom tags that you can develop in JSP are :

  • Empty tags: Refer to the custom tags that do not have any attribute or body. The following code snippet shows an empty custom tag:

  <td:welcome />

  • Tags with attributes: Refer to custom tags for which you can define attributes to customize the behavior of the custom tag. The following code snippet shows a custom tag with an attribute color:

  <td: welcome color=?blue?></td:welcome>

  •  Tags with a body: Refer to the custom tag within which you can define nested custom tags, scripting elements, actions, HTML text,   and JSP directives. The following code snippet shows a custom tag that contains a JSP scripting element as its body:

 <td: welcome>

 <%=today_date%>

 </td:welcome>

  •  Nested tags: Refer to the set of custom tags in which one custom tag encloses one or more custom tags. The following code snippet shows a nested custom tag:

 <td1:ifTag condition ?<%=eval>? >

     <td2:valueTrue>

       The expression evaluates to true

     </td2:valueTrue>

 </td1:ifTag>/font>

Development of Custom Tag :

TTo develop a custom tag, you need to perform following steps:

  •  Develop a tag handler or class file(.java)

  • Develop the Tag Library Descriptor (TLD) file

  •  Include the Tag Library in a JSP page

  • Deploy the application

EXAMPLE : In this Example, we are developing a /p>

Tag Handler File : & Build a class that implements the javax.servlet.jsp.tagext  tag interface as follows. Compile it and place it under the web-inf/classes directory (in the appropriate package structure). 

/* Imports the package needed to implement the custom tag *//font>
package copyright;
iimport javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/* Extending the TagSupport interface */
public class CopyrightTag extends TagSupport
{
  /* Invokes when the start tag of the custom tag is encountered */
  public int doStartTag() throws JspException
  {
    try
    {
      /* Creating an instance of the JSPWriter for displaying the output */
      JspWriter out=pageContext.getOut();
      out.println("<BR><B>Copyright RoseIndia Technologies<B>");
    }
    catch (Exception ioException)
    {
      System.err.println("IO Exception");
      System.err.println("ioException.toString()");
    }
    /* Returning the SKIP_BODY, as the body content is not be evaluated */
    return SKIP_BODY;
  }
  /* Invokes when the end tag of the custom tag is encountered */
  public int doEndTag() throws JspException
  {/font>
      /* Skip the processing of the rest of the page */
      return SKIP_PAGE;
   }
}

Tag Library Descriptor (TLD) file :  NNow we need to describe the tag, so create a file called CopyrightTag.tld and place it under the web-inf directory.

<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>Copyright Info</short-name>
<uri>/CopyrightTag.tld</uri>
<description>
Developing First Custom Tags
</description>
<tag>
<name>CopyrightTag</name>
<tag-class>copyright.CopyrightTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>

JSP File :

<html>
  <head>
    <title>New Tech Books </title>
  </head>
  <body>
    <center> <h1> <font color="blue">Roseindia Technologies </font></h1></center>
    <font color="red"><center><h2> Welcome to our Website</h2></center>
    
    <%taglib uri="/CopyrightTag.tld" prefix="chrt" %>
    <center><chrt:CopyrightTag /></center>
    </font>/font>
  </body>
</html>

Deployment of application (OUTPUT) :

Download Source Code

Advertisements

Advertisement

Posted on: June 28, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.