The servlet container creates a ServletResponse object and passes it as an argument to the servlet's service method. To send binary data in a MIME body response, use the ServletOutputStream returned by getOutputStream() . To send character data, use the PrintWriter object returned by getWriter() .
getWriter. Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding() .
The general rule of them is this: if you opened the stream, then you should close it. If you didn't, you shouldn't. Make sure the code is symmetric. In the case of HttpServletResponse , it's a bit less clear cut, since it's not obvious if calling getOutputStream() is an operation that opens the stream.
setContentType. Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8 .
Why is it that on an instance of ServletResponse
both getWriter()
and getOutputStream()
can't be called?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With