To add a JSP custom label:
Add a TLD file to the Web-inf folder first
<?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 web-jsptaglibrary_2_0.xsd"
version= "2.0" >
<description>myTag</description>
<display-name>jstl core</display-name>
<tlib-version>1.0</tlib-version>
<short-name>cnweb</short-name>
<uri>http://www.cnweb.cn</uri>
<tag><!--a tag tag corresponding to a custom tag tag class-->
<description>MyTag</description>
<name>when</name>
<tag-class>cn.example.when</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
Reference rule:
<%@ taglib uri= "http://www.cnweb.cn" prefix= "Cnweb"%>
To define an error-handling page:
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errors/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/errors/error1.jsp</location>
</error-page>
Custom Label Execution Process:
Simpletagsupport
/* The JSP engine encounters a simple class that instantiates the class;
* Call Setjspcontext, the page PageContext pass to the label processing class;
* Call SetParent to pass the parent sign in, if not, pass NULL
* Call the Setjspbody method to pass the encapsulated tag body jspfragment to the label processor class
* Execute the Custom label on the page, Dotag () method;--> execution complete, destroy object
*/
Jspfragment JF = This.getjspbody ();
Jf.invoke (This.getjspcontext (). Getout ());//Do not process if not shown
--------------------------------------------------------------------
public class Tagshowornot extends TagSupport {
public int doStartTag () throws Jspexception {
return tag.eval_body_include;//Show Body
Return tag.skip_body;//Hide Body
tag.eval_page;//Display PAGE
tag.skip_page;//Hide PAGE
}}
TagSupport
/* Write a class that implements TagSupport;
* Describe the label processor class in the TLD file (location of the TLD file: Web-inf)
* Import and use labels in JSP pages
*
*jsp when a custom label is encountered during execution, the class is instantiated first;
* Then Execute method: Setpagecontext ()-->setparent ()-->dostarttag ()
* If there is a label body, the general execution of the label body; Doendtag ()--> the entire label is usually performed after execution: release ()
*
* Controls whether the entire JSP page executes;
* Control the content of a part of the JSP page is implemented;
* Control JSP page content repeated execution;
* Modify JSP page content output;
*/
After executing the label body, the method executes before executing the Doendtag () method until the method returns Iterationtag.skip_body;
public int doafterbody () throws Jspexception {
time--;
System.out.println ("repeat" + time);
if (Time > 0) {
return iterationtag.eval_body_again;
} else
return iterationtag.skip_body;
}