JSP custom tag Learning (advanced)
Keyword: custom tag
Bodytag Interface
The implementation class of the bodytag interface, which is used to operate the body content (bodycontent ).
Definition method: Public void doinitbody ();
Public int doatferbody ();
Tip: The methods called after the dostarttag () method depend on the return value of the dostarttag () method and check whether the action element is customized on the JSP page, instead of how to declare it in the TLD file.
In a TLD file, if <body-content> Empty </body-content>, the dostarttag () method can only return skip_body
Body-content has three optional values: Empty, JSP, and tagdependent.
If no value is set, dostarttag () returns skip_body, eval_body_include, or eval_body_buffered.
Description of returned values:
If skip_body is returned, the body is not evaluated and the doendtag () is called;
If eval_body_include is returned and the custom action element is not empty, doinitbody () and setbodycontent () will not be called, while body evaluated and "pass" the current out object, when the action element is empty, only dostarttag () and doendtag () are available ();
When eval_body_buffered is returned and the custom action element is not empty, setbodycontent (), doinitbody () is called, body is evaluated, and doafterbody () is called, doendtag () is called after more than 0 iterations, and when the action element is empty, only dostarttag (), doendtag ();
Subject content
1. Custom tags usually require you to manipulate your own subject content. For example, for database queries, You need to interpret the topic content as SQL
2. Working methods
The servlet container contains the bodycontent object stack. The servlet container calls pagecontext after the dostarttag () method. pushbody () is to push the current body content into the stack, where the out variable is redirected to the content of the current body content instance topic, retaining a reference to the jspwriter object, that is, the previous output, jspwriter writes data to the response stream. in this way, the writer stack is created effectively.
The servlet container calls the pagecontent. popbody () method between the doafterbody method and the doendtag method.
Note that do not access bodycontent after doendtag (), because the servlet container may have used the topic content of the TAG again until the doendtag () method of the tag handler is called, using the doendtag method to access the tag of your subject content may access the content of other marked subjects, or access null values, leading to uncertain behavior.
Bodytagsupport class that implements the bodytag Interface
Public bodycontent getbodycontent (); // return the topic content
Public jspwriter getpreviousout (); // returns the previous out object
What's the purpose of talking about so many things? Let's write something to illustrate the following:
Print the subject content! Java code
- ImportJava. Io. ioexception;
- ImportJavax. servlet. jsp. jspexception;
- ImportJavax. servlet. jsp. tagext. bodytagsupport;
- Public ClassPrintbodytagExtendsBodytagsupport {
- Public IntDoaftertag ()ThrowsJspexception
- {
- Try
- {
- This. Getbodycontent (). writeout (getpreviousout (); // The theory mentioned above!
- }Catch(Ioexception E)
- {
- Throw NewJspexception (E );
- }
- ReturnSkip_body;
- }
- }
Deploy TLD file XML Code
- ......
- <Tag>
- <Name> printbody </Name>
- <Tag-class> printbodytag </Tag-class>
- </Tag>
- ......
Page loading:
HTML code
- <% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "ISO-8859-1" %>
- <% @ Taglib uri = "http://www.eimhe.com/taglibs/util.tld" prefix = "util" %>
- <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
- <HTML>
- <Head>
- <Title> my JSP 'tag. jsp 'starting page </title>
- </Head>
- <Body>
- <Util: printbody> aaaaaaaa </util: printbody> // The result is how you can guess it! Aaaaaaaa
- </Body>
- </Html>