Jspwriter and printwriter)

Source: Internet
Author: User

1 All inherit from Java. Io. Writer class

Jspwriter can directly use the out object output on the JSP page, or use pagecontex. getout () to obtain the out object.

Printwriter must be obtained through response. getwriter ()

2. Use two methods to output data simultaneously on the JSP page. Data in printwriter is output first.

For example:

Create a test. jsp page

<%
Out. println ("out ");
Jspwriter out1 = pagecontext. getout ();
If (out = out1)
{
Out. println ("out = out1 ");
}
Else
{
Out. println ("out! = Out1 ");
}

Printwriter PW = response. getwriter ();
PW. Write ("PW writer ");
%>

The running result is

PW writer out = out1 This Is My JSP page.

Cause:

When an out object is inserted to the buffer before printwriter, The. Out object meets certain conditions.

To call the print () method of the printwriter object and output the content in the out buffer to the browser.

If you wantCodeThe output can be written as follows:

Out. Flush ();

Refresh the cache.

The output result is changed

Out out = out1 PW writer This Is My JSP page

 

 

In addition, the print method of printwriter does not throw ioexception, whereas jspwriter does.
Jspwriter is an abstract class, but printwriter is not. That is to say, you can create a printwriter object directly through the new operation, but jspwriter cannot. It must be created through its subclass.
But there is a relationship between them. This relationship is mainly because jspwriter has dependencies on printwriter. When initializing a jspwriter object, you need to associate it with a printwriter type object of the servletresponse object. In the end, the output task of the jspwriter object is still performed through the printwriter type object.

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.