Share on Google+Share on Google+

Display Current Date using JSP Custom(user define) Tag

Advertisement
In this section, we will discuss about how to display current date & time using custom(user define) tag.

Display Current Date using JSP Custom(user define) Tag

In this section, we will discuss about how to display current date & time using  custom(user define) tag.

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>

Development of Custom Tag :

To 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, Current date & time will display using custom tag.

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). 

ShowDateTag.java
package foo;

import java.io.*;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class ShowDateTag implements Tag {
  
  private PageContext pageContext;
  private Tag parent;
  
  public int doStartTag() throws JspException {
    return SKIP_BODY;
  }
  
  public int doEndTag() throws JspException {
    try {
      pageContext.getOut().write("" new java.util.Date());
    catch (IOException ioe) {
      throw new JspException(ioe.getMessage());
    }
    
    return EVAL_PAGE;
  }
  
  public void release() {
  }
  
  public void setPageContext(PageContext page) {
    this.pageContext = page;
  }
  
  public void setParent(Tag tag) {
    this.parent = tag;
  }
  
  public Tag getParent() {
    return this.parent;
  }
  
}

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

<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>myTag</shortname>
<uri>http://www.roseindia.net/taglib</uri>
<info>My own tag library</info>

<tag>
<name>showDate</name>
<tagclass>foo.ShowDateTag</tagclass>
<info>Show the current date</info>
</tag>

</taglib>

JSP file :

<html>

<head><title>Date tag example</title></head>

<body>
<h1><font color="red">TIME IS NOW:
<br>
<%@ taglib uri="http://www.roseindia.net/taglib" prefix="myTag" %>

<myTag:showDate/>
</font></h1>
</body>
</html>

Deployment of application (OUTPUT) :

Download Source Code

Advertisements

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.