Tip: simple tag
Five methods for defining simple labels:
1. setjspcontext Method
It is used to pass the pagecontext object of the JSP page to the tag processor object.
2. setparent Method
Used to pass the parent Tag Processor object to the current Tag Processor object
3. getparent Method
Parent Tag Processor object used to obtain the current tag
4. setjspbody Method
It is used to pass the jspfragment object representing the TAG body to the tag processor object.
5. dotag Method
It is used to complete all tag logic, including output, iteration, and modification of TAG body content. Javax can be thrown in the dotag method. servlet. JSP. skippageexception indicates that the Web Container no longer executes the content after the ending tag in the JSP page, which is equivalent to returning the tag in the doendtag method of the traditional tag. skip_page constant.
Tip: simple tag interface method execution sequence:
1. When the Web Container starts to execute tags, the following method is called to complete tag initialization:
(1) The Web Container calls the setjspcontext method of the tag processor object to pass the pagecontext object representing the JSP page to the tag processor object.
(2) The Web Container calls the setparent method of the tag processor object and passes the parent Tag Processor object to the tag processor object. Note that the Web Container will call this method only when the tag has a parent tag.
(3) If an attribute is set when a tag is called, the container will call the setter method corresponding to each attribute to pass the attribute value to the tag processor object. If the attribute value of a tag is an El expression or a script expression, the Web Container first calculates the value of the expression and passes the value to the tag processor object.
(4) If a simple label has a label body, the container will call the setjspbody method to pass in the jspfragment object representing the label body.
2. When executing tags:
The container calls the dotag () method of the tag processor. by operating the jspfragment object in the method body, developers can execute, iterate, and modify the TAG body.
Tip: jspfragment class
1. javax. servlet. JSP. tagext. the jspfragment class is defined in jsp2.0. Its instance object represents a JSP segment on the JSP page that complies with the JSP syntax specifications. This JSP segment cannot contain JSP script elements.
2. when processing a simple tag body, the Web Container uses a jspfragment object to represent the content of the TAG body, call the setjspbody method of the tag processor object to pass the jspfragment object to the tag processor object. Only two methods are defined in the jspfragment class, as shown below:
3. getjspcontext Method
A) return the jspcontext object representing the called page.
4. publicabstract void invoke (Java. Io. Writer out)
• Used to execute JSP code snippets represented by jspfragment objects
• The parameter out is used to specify the output stream object to which the execution result of the jspfragment object is written. If the value passed to the parameter out is null, the execution result is written to jspcontext. in the output stream object returned by the getout () method. (In short, it can be understood as writing to a browser)
Tip: invoke Method
This method can be used to control whether to execute and output the content of the label body, whether to iteratively execute the content of the label body, or modify the execution result of the label body before outputting.
If the jspfragment. Invoke method is not called in the tag processor, the result is equivalent to ignoring the TAG body content;
If the jspfragment. Invoke method is repeatedly called in the tag processor, the TAG body content will be repeated;
To modify the content of the TAG body in the tag processor, you only need to specify an output stream object (such as stringwriter) that can retrieve the result data when calling the invoke method ), let the execution result of the TAG body be output to the output stream object. Then, extract the data from the output stream object and modify the data before outputting the data to the target device. This allows you to modify the TAG body.
Import java. Io. ioexception;
Import javax. servlet. jsp. jspexception;
Import javax. servlet. jsp. tagext. jspfragment;
Import javax. servlet. jsp. tagext. simpletagsupport;
Publicclass simpletagdemo extends simpletagsupport {
@ Override
Public void dotag () throws jspexception, ioexception {
// Todo auto-generated method stub
// The system automatically calls and encapsulates the content in the TAG body into an object.
Jspfragmentjf = This. getjspbody (); // obtain the TAG body object.
JF. Invoke (this. getjspcontext (). getout (); // output to the client
// JF. Invoke (null); // output to the client
Super. dotag ();
}
}
ScriptlessNo script.
Control iteration with simple tags
1)Create a Tag Processor
2)Description in TLD File
// Display the content in the label body 10 times
Jspfragment JF =This. Getjspbody ();
For(IntI = 0; I <10; I ++ ){
JF. Invoke (Null);
}
Flexible parameter passing and loop
Required to set some attributes in the TLD File
<Attribute>
<Name> count </Name>
<Required> true </required> // set whether the attribute is required.
<Rtexprvalue> true </rtexprvalue> // whether runtime expression computing is supported <csdn: demo8 COUNT = "$ {date}"> or <csdn: demo8 COUNT = "<% = 5 + 8%>">. You can write true to false, or assign a value to count when writing true.
</Attribute>
Simpletagdemo8.java
Private intCount;
Public voidSetcount (IntCount ){
This. Count = count;
}
@ Override
Public voidDotag ()ThrowsJspexception, ioexception {
Jspfragment JF =This. Getjspbody ();
For(IntI = 0; I <count; I ++ ){
JF. Invoke (Null);
}
Tagdemo8.jsp
<Csdn: demo8 COUNT ="5">
This is my JSP page. <br>
</Csdn: demo8>
Modify the label body with simple tag Control
Jspfragment JF =This. Getjspbody (); // obtain the TAG body.
// Save to the buffer
// Stream object that can obtain the content in the buffer zone
Stringwriter Sw =NewStringwriter ();
JF. Invoke (SW); // The parameter in the brackets is the stream object, and the content on the label is obtained through the stream object.
String content = Sw. tostring (). touppercase (); // converts lowercase letters to uppercase letters.
// Get the output stream object
Jspwriter JW =This. Getjspcontext (). getout ();
JW. Write (content );
How to Develop tags with attributes:
<Csdn: demo8 COUNT = "$ {4 + 1}"> </csdn>
The tag processor must also contain a private attribute count and setter method.
Dotag () contains the business logic code
TLD file:
<Tag>
<Name> demo9 </Name>
<Tag-class> com. hbsi. Tag. simpletagdemo9 </Tag-class>
<Body-content>Scriptless</Body-content>
<Attribute>
<Name> count </Name>
<Required> true | false </required> // set whether the attribute is required.
<Rtexprvalue> true | false </rtexprvalue>
</Attribute>
</Tag>
Anti-leech: <Dhw: referrer site = http: // localhost/>
// For a website that provides anti-leech protection, if the page is leeching, it will jump to a page
Referertag
ImportJava. Io. ioexception;
ImportJavax. servlet. http. httpservletrequest;
ImportJavax. servlet. http. httpservletresponse;
ImportJavax. servlet. jsp. jspcontext;
ImportJavax. servlet. jsp. jspexception;
ImportJavax. servlet. jsp. pagecontext;
ImportJavax. servlet. jsp. skippageexception;
ImportJavax. servlet. jsp. tagext. simpletagsupport;
Public classReferertagExtendsSimpletagsupport {
PrivateString site;
PrivateString page;
@ Override
Public voidDotag ()ThrowsJspexception, ioexception {
//TodoAuto-generatedmethod stub
//JCYesPCSubclass
Jspcontext JC =This. Getjspcontext ();
Pagecontext Pc = (pagecontext) JC;
Httpservletrequest request = (httpservletrequest) PC. getrequest ();
String Referer = request. getheader ("Referer"); // get the Request Message Header
// Redirect to the homepage
// Obtain the response object
Httpservletresponse response = (httpservletresponse) PC. getresponse ();
If(Referer =Null|! Referer. startswith (SITE )){
// Leeching, jump to the home page, the content behind the current page is not processed, all throwing exceptions
// Get the context object and jump to the specified page
If(Page. startswith (request. getcontextpath ())){
Response. sendredirect (PAGE );
}Else if(Page. startswith ("/")){
Response. sendredirect (request. getcontextpath () + page );
}Else{
Response. sendredirect (request. getcontextpath () + "/" + page );
}
Throw newSkippageexception ();
}
}
Public voidSetsite (string site ){
This. Site = site;
}
Public voidSetpage (string page ){
This. Page = page;
}
}
Testreferer. jsp Section
<% @ Taglib uri =Http://www.dhw.com"Prefix ="Dhw"%>
<Dhw: Referer site ="Http: // localhost"Page ="/Index. jsp"/>
<Body>
Star info -------- star info
</Body>
Index. jsp:
<A href = "/20111108/example/testreferer. jsp"> Star archives </a>
Dhw. TLD part:
<Taglib>
<Tlib-version> 1.0 </tlib-version>
<JSP-version> 1.2 </JSP-version>
<Short-Name>Dhw</Short-Name>
<URI> http://www.dhw.com </uri>
<Tag>
<Name>Referer</Name>
<Tag-class> com. hbsi. Tag. referertag </Tag-class>
<Body-content> Empty </body-content>
<Attribute>
<Name> site </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
<Attribute>
<Name> page </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>
</Taglib>