Take the tag for obtaining the current time as an example to develop a custom tag bin: currentTime. The procedure is as follows:
1. First define a tag processing class.
[Java]
Package org. binbin. tag;
Import java. io. IOException;
Import java. text. DateFormat;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;
/**
* Custom tags
* @ Author binbin (cn.binbin@qq.com)
* @ Date 2013-3-28
*/
Public class TimeTag extends TagSupport
{
Private static final long serialVersionUID = 6918846280074418825L;
@ Override
Public int doStartTag () throws JspException
{
JspWriter out = this. pageContext. getOut ();
// Define the time output format
DateFormat df = new SimpleDateFormat ("yyyy/MM/dd HH: mm: ss ");
// Obtain the specified format string for the current time
String str = df. format (new Date ());
Try
{
// Output the current time string to the page
Out. print (str );
} Catch (IOException e)
{
// If an exception occurs, the current program cannot handle it. Therefore, a runtime exception is thrown again.
Throw new RuntimeException (e );
}
Return super. doStartTag ();
}
}
Package org. binbin. tag;
Import java. io. IOException;
Import java. text. DateFormat;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;
/**
* Custom tags
* @ Author binbin (cn.binbin@qq.com)
* @ Date 2013-3-28
*/
Public class TimeTag extends TagSupport
{
Private static final long serialVersionUID = 6918846280074418825L;
@ Override
Public int doStartTag () throws JspException
{
JspWriter out = this. pageContext. getOut ();
// Define the time output format
DateFormat df = new SimpleDateFormat ("yyyy/MM/dd HH: mm: ss ");
// Obtain the specified format string for the current time
String str = df. format (new Date ());
Try
{
// Output the current time string to the page
Out. print (str );
} Catch (IOException e)
{
// If an exception occurs, the current program cannot handle it. Therefore, a runtime exception is thrown again.
Throw new RuntimeException (e );
}
Return super. doStartTag ();
}
}
2. Then add a tag library definition file bin. tld under the WEB-INF directory
[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: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
Version = "2.0">
<Tlib-version> 1.0 </tlib-version>
<Short-name> bin </short-name>
<Uri>/bin-tags </uri>
<Tag>
<Name> currentTime </name>
<Tag-class> org. binbin. tag. TimeTag </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: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
Version = "2.0">
<Tlib-version> 1.0 </tlib-version>
<Short-name> bin </short-name>
<Uri>/bin-tags </uri>
<Tag>
<Name> currentTime </name>
<Tag-class> org. binbin. tag. TimeTag </tag-class>
<Body-content> empty </body-content>
</Tag>
</Taglib>
3. Use the tag on the jsp page
[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! -- Import the corresponding tag Library first -->
<% @ Taglib uri = "/bin-tags" prefix = "bin" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> My JSP '1. jsp 'starting page </title>
</Head>
<Body>
Current System Time: <bin: currentTime/>
</Body>
</Html>
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! -- Import the corresponding tag Library first -->
<% @ Taglib uri = "/bin-tags" prefix = "bin" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> My JSP '1. jsp 'starting page </title>
</Head>
<Body>
Current System Time: <bin: currentTime/>
</Body>
</Html>
Iv. Running Effect