JSP is better than ASP. It not only achieves the separation of the presentation layer and the business logic layer, but also provides the custom tag function, which greatly facilitates our development, this is conducive to the maximum reuse of components!
Next, I will use a custom tag to display the date and write out the process of implementing the custom tag of JSP:
1: Compile the tag handler class. This class must implement the javax. servlet. jsp. tagext. Tag interface, which is usually implemented by extending the tagsupport or bodytagsupport class.
Datetag. Java (TAG processing class)
-----------------------------------------------------
Package test;
Import javax. servlet. jsp. jspexception;
Import javax. servlet. jsp. tagext .*;
Import javax. servlet. jsp .*;
Import java. util .*;
Import java. Text .*;
Import java. Io. ioexception;
Public class datetag extends tagsupport {
Private string format = "mm DD, YYYY ";
Public int dostarttag () throws jspexception {
Simpledateformat SDF = new simpledateformat (format );
Date = new date ();
String formatdate = SDF. Format (date );
Try {
Jspwriter out;
Out = pagecontext. getout ();
Out. println (formatdate );
} Catch (ioexception E)
{
Throw new jspexception ("I/O error! "+ E. getmessage ());
}
Return eval_body_include;
}
Public String getformat (){
Return format;
}
Public void setformat (string format ){
This. format = format;
}
}
The main function of this class is to output the current date string to the browser.
2: Create a tag library descriptor File
Datetag. TLD
---------------------------------------------------------------------
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype taglib public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.1 //" http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd ">
<Taglib>
<Tlibversion> 1.2 </tlibversion>
<Jspversion> 1.1 </jspversion>
<Shortname> bean </shortname>
<URI> http://www.erapidsoft.com </uri>
<Tag>
<Name> displaydate </Name>
<Tagclass> test. datetag </tagclass>
<Attribute>
<Name> Format </Name>
<Required> true </required>
</Attribute>
</Tag>
</Taglib>
The name attribute of the <tag> element in this file specifies the name of the tag. You can see it in the following JSP file for use.
The tagclass attribute specifies the path of the processed class.
<Attribute> the child element specifies the parameter of this tag. The name attribute specifies the parameter name. The require attribute indicates whether this parameter is required. If it is true, this label must be set to the format value.
3: add the <taglib> element to Web. XML to make the TLD file and handler class accessible.
<Taglib>
<Taglib-Uri> mytags </taglib-Uri>
<Taglib-location>/WEB-INF/datetag. TLD </taglib-location>
</Taglib>
Here, <taglib-Uri> matches the uri of the taglib command of the JSP file, and <taglib-location> indicates the path of the marked definition file.
4: Create a JSP page and use the tag
Hello. jsp
-----------------------------------------
<% @ Taglib uri = "mytags" prefix = "wasingmon" %>
<HTML>
<Body>
<Wasingmon: displaydate format = "yyyy-mm-dd"/>
</Body>
</Html>
When using tags, you must use <% @ taglib %> to introduce the used tag library. prefix indicates the tag prefix and can be specified arbitrarily;
Finally, enter and request this page in the browser to display the current date.
End!