1. First define a class that implements the Label function (Mytagclass.java)
public class Mytagclass extends TagSupport {
@Override//Override doStartTag () method
public int doStartTag () {
HttpServletRequest request;//is a property defined in the TagSupport class, which is an object of Javax.servlet.jsp.PageContext
Request = (HttpServletRequest) pagecontext.getrequest ();
SimpleDateFormat formater = new SimpleDateFormat ("Yyyy-mm-dd");
String strdate = Formater.format (New Date ());
JspWriter out = Pagecontext.getout ();
try {
Out.print (strdate);
} catch (Exception e) {
E.printstacktrace ();
}
The return skip_body;//doStartTag () method returns Skip_body. The reason, of course, is that our simple date tag has no body.
}
}
2. Create a new tag library definition file (MYTAGLIB.TLD)
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>my</short-name>
<tag>
<name>displaydate</name>//Tag name, custom, map to a class
<tag-class>day01. Mytagclass</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
3. Adding nodes to the Web. xml file
<jsp-config>
<taglib>
<taglib-uri>myTagLib</taglib-uri>//Tag library file identification, map to tag library file
<taglib-location>/web-inf/mytaglib.tldThe actual address of the </taglib-location>//tag library file
</taglib>
</jsp-config>
4. Add the TAGLIB directive to the JSP file and add a custom label to the body
<%@ taglib uri= "Mytaglib" prefix= "CC"%>//urifor tag library file identification, prefix tag prefix
<body>
<cc:displayDate/>//Find the displaydate tag from the tag library specified by Mytaglib, and then find the class that corresponds to the tag name and execute the doStartTag () method.
</body>
For more information, please refer to: http://blog.csdn.net/zyujie/article/details/8735730