Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlets in java - both getWriter() and getOutputStream()

Tags:

People also ask

What is PrintWriter and getWriter in servlet?

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() .

What is the purpose of the getWriter () method of ServletResponse?

getWriter. Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding() .

Should I close HttpServletResponse Outputstream?

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.

What is the use of setContentType in servlet?

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?