The following tag handler is used to respond to doTag() method and sends system date and time back to JSP in which it is placed. WEB-INF/classes/st/CurrentTime.java
package st; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import java.util.Date; public class CurrentTime extends SimpleTagSupport { public void doTag() throws JspException , IOException { JspWriter out = getJspContext().getOut(); out.println( new Date().toString()); } } // end of classCurrentTime class extends SimpleTagSupport, which is implementing SimpleTag interface.
Compile tag handler by first setting path and classpath as follows:
path c:\jdk1.4.2\bin;c:\windows\system32 set classpath=.;c:\tomcat5\common\lib\servlet-api.jar;c:\tomcat5\common\lib\jsp-api.jar;Note: You have to change path of JDK and Tomcat according to your system.
SimpleTag interface contains the following other methods:
The following TLD file provides details regading the library such as its version and the version of JSP it needs etc and tags.
WEB-INF/st.tld
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>st</short-name> <tag> <name>time</name> <tag-class>st.CurrentTime</tag-class> <body-content>empty</body-content> </tag> <tag> <name>formattime</name> <tag-class>st.FormatCurrentTime</tag-class> <body-content>empty</body-content> <attribute> <name>format</name> <required>false</required> </attribute> </tag> </taglib>
The following JSP registers st.tld and uses time tag with prefix st.
<%@ taglib uri="/WEB-INF/tlds/st.tld" prefix="st"%> <st:time/>
In order to provide an attribute, we have to modify tag handler to incude a property with the same name as the attribute. If user provides a value to property format then its value is used in format method of SimpleDateFormat class. The following is the new code with a property and extra process added.
FormatCurrentTime.java
package st; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import java.util.Date; import java.text.SimpleDateFormat; public class FormatCurrentTime extends SimpleTagSupport { private String format=null; public void setFormat(String format) { this.format = format; } public void doTag() throws JspException , IOException { JspWriter out = getJspContext().getOut(); if ( format != null) { SimpleDateFormat sdf = new SimpleDateFormat( format); out.println( sdf.format( new Date())); } else out.println( new Date().toString()); } } // end of classIn order to add an attribute to a tag, modify TLD to include details regarding format attribute as shown below. Tag element is to be added after the previous tag. The complete code is shown below.
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>st</short-name> <tag> <name>time</name> <tag-class>st.CurrentTime</tag-class> <body-content>empty</body-content> </tag> <tag> <name>formattime</name> <tag-class>st.FormatCurrentTime</tag-class> <body-content>empty</body-content> <attribute> <name>format</name> <required>false</required> </attribute> </tag> </taglib>Attribute element specifies the details of attributes like name of the attribute and whether or not the attribute is mandatory. If required element is set to true then attribute becomes mandatory.
The following is the code for time.jsp. It shows how to use time tag and also formattime tag.
<%@ taglib uri="/WEB-INF/tlds/st.tld" prefix="st"%> <st:time/> <p> <st:formattime /> <p> <st:formattime format="dd/MMM/yy"/>