Web Project, New Java class:
[Java] package leon. webtest. tags;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;
Public class DateTime extends TagSupport {
Private static final long serialVersionUID = 1L;
Public int doStartTag () throws JspException {
Try {
JspWriter out = this. pageContext. getOut ();
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");
Out. println ("<span> ");
Out. println (sdf. format (new Date ()));
Out. println ("</span> ");
} Catch (Exception e ){
Throw new JspException (e. getMessage ());
}
Return SKIP_BODY;
}
}
Package leon. webtest. tags;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;
Public class DateTime extends TagSupport {
Private static final long serialVersionUID = 1L;
Public int doStartTag () throws JspException {
Try {
JspWriter out = this. pageContext. getOut ();
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");
Out. println ("<span> ");
Out. println (sdf. format (new Date ()));
Out. println ("</span> ");
} Catch (Exception e ){
Throw new JspException (e. getMessage ());
}
Return SKIP_BODY;
}
}
New tld file: WEB-INF/tld/webtest-tags.tld
[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<Taglib xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalLocation = "http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
Version = "2.0">
<Description> A tag library exercising SimpleTag handlers. </description>
<Tlib-version> 1.0 </tlib-version>
<Short-name> Examples </short-name>
<Uri>/MyTags </uri>
<Description> JSP Self-define Tag Library. </description>
<Tag>
<Description> This is an date time tag </description>
<Name> datetime </name>
<Tag-class> leon. webtest. tags. DateTime </tag-class>
<Body-content> empty </body-content>
</Tag>
</Taglib>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Taglib xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalLocation = "http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
Version = "2.0">
<Description> A tag library exercising SimpleTag handlers. </description>
<Tlib-version> 1.0 </tlib-version>
<Short-name> Examples </short-name>
<Uri>/MyTags </uri>
<Description> JSP Self-define Tag Library. </description>
<Tag>
<Description> This is an date time tag </description>
<Name> datetime </name>
<Tag-class> leon. webtest. tags. DateTime </tag-class>
<Body-content> empty </body-content>
</Tag>
</Taglib>
Configuration in web. xml:
[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app id = "WebApp_ID" version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<Display-name> WebTest </display-name>
<Welcome-file-list>
<Welcome-file> index.html </welcome-file>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
<Jsp-config>
<Taglib>
<Taglib-uri>/MyTags </taglib-uri>
<Taglib-location>/WEB-INF/tld/webtest-tag.tld
</Taglib-location>
</Taglib>
</Jsp-config>
</Web-app>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app id = "WebApp_ID" version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<Display-name> WebTest </display-name>
<Welcome-file-list>
<Welcome-file> index.html </welcome-file>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
<Jsp-config>
<Taglib>
<Taglib-uri>/MyTags </taglib-uri>
<Taglib-location>/WEB-INF/tld/webtest-tag.tld
</Taglib-location>
</Taglib>
</Jsp-config>
</Web-app>
Use in JSP:
First, import:
<% @ Taglib uri = "/MyTags" prefix = "leon" %>
Use tags:
<Leon: datetime/>
Enhanced version: The time to be displayed is dynamic and will jump in seconds like a clock.