Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpServletResponse - writing to response writer vs outputstream

Tags:

java

servlets

When adding to some content to HttpServletResponse in java I can either get the response writer and append:

httpResponse.getWriter().append("Some Content");

Or I can add content to the output-stream:

ServletOutputStream servletOut = httpResponse.getOutputStream();
servletOut.write(someByteArray);

Is the only difference between the two is that the first gets strings/char-sequences and the second gets bytes (of course the content-type is affected as well)? Should I prefer one over the other? When should I use which?

like image 535
forhas Avatar asked Nov 28 '25 01:11

forhas


1 Answers

Below table shows difference between them, You can use any of these based on requirement which fits in the table.

enter image description here

like image 99
Vivek Gupta Avatar answered Nov 29 '25 13:11

Vivek Gupta