Difference between response. getWriter (). write () and out. print (), response. getwriter
1. first introduce the differences between the write () and print () methods:
(1), write (): only supports output of character type data, character, character array, String, etc.
(2) print (): Data of various types (including objects) can be converted to bytes by default encoding. These bytes are output through the write (int c) method.
2. Introduce the differences between response. getWriter () and out:
(1) The out and response. getWriter classes are different. One is JspWriter and the other is java. io. PrintWriter.
(2) Different execution principles: JspWriter is equivalent to a printWriter with the cache function. Instead of directly outputting data to the page, it refreshes the data to the response Buffer and then outputs the data,
Response. getWriter directly outputs data (response. print (), so (out. print) can only be output after it.
(3) The out object is a built-in jsp object. Refresh the jsp page and obtain the out object automatically after initialization. Therefore, you need to refresh the page when using the out object,
The response information of response. getWriter () is output to the webpage through the out object. When the response ends, it is automatically disabled and is irrelevant to the jsp page. You do not need to refresh the page.
Image metaphor: When we call the response. getWriter () object to get the paint brush for the web page, you can use this paint brush to draw anything you want to display on the web page.
(4) The print () method of out and the println () method will generate an ioexception when the buffer overflow is not automatically refreshed,
The print and println of the response. getWrite () method both suppress the ioexception and do not have ioexception.
Out. println (""); the method cannot wrap the page layout. It can only wrap the html code. to wrap the page layout, you can: out. println ("</br> ");
Article Reprinted from: http://blog.csdn.net/javaloveiphone/article/details/8133772
What is the difference between print and write in the out object method of servlet?
In common: both of them do not refresh the new page, but only write data on the original page. In the end, the write method in the abstract class Writer is overwritten.
The print method can convert various types of data into strings for output. The overloaded write method can only output character-related data, such as characters, character arrays, and strings.
What is the difference between outprint () and outwrite () in servlet?
Out. write () is a byte output stream method.
Out. print () is the method of character output stream
In servelt, it is the character output stream obtained by HttpServletResponse. getWriter ().
Jsp implicit variable out is also a character output stream. Therefore, the print () method is also available.