Relationship between jspwriter and printwriter:

Source: Internet
Author: User

1. All are inherited from the java. Io. Writer class.

Jspwriter can directly use the out object output on the JSP page. You can use pagecontext. getout (); to obtain the jspwriter object.
Printwrietr must use response. getwriter () in the JSP page to obtain its object. The two have different scopes.

2. Call two methods to output data to the page at the same time on the JSP page. The data in printwriter is output before jspwriter.

For example:

Jspwriter out1 = pagecontext. getout (); // you can directly use the out object on the JSP page instead
Example: Use pagecontext. getout (); to obtain the jspwriter object. In this way, out1 is the same as the out object encapsulated in the JSP page.
An object. (You can use out = out1 to determine whether two objects are equal .)

Out1.print ("222 ");
Printwriter PW = response. getwriter ();

PW. Print ("111 ");

* In this case, "111" is displayed on the browser, and "222" is displayed. It indicates that no matter whether jspwriter and printwriter areProgramHow is the order in?The data in pringwriter is always output first and then the data in jspwriter is output. This is because the out object is equivalent to inserting it into the buffer zone before printwriter.When an out object meets certain conditions, the print () method of the printwriter object is called to output the content in the out buffer to the browser.CodeThe output can be written as follows:

Spwriter out1 = pagecontext. getout ();

Out1.print ("222 ");

Out1.flush (); // refresh the buffer

Printwriter PW = response. getwriter ();

PW. Print ("111 ");

* Conditions for the out object to call the print () method of the printwriter object to output content in the buffer:

^ Set the buffer attribute of the page command to disable the cache function of the out object.
^ The content written to the out object is filled with the buffer of the out object
^ The entire JSP page ends.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.