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.