As Heikki said, setting status instead of sendError() causes Tomcat not to touch the object / body / payload of the response.
If you want to send response headers without any entity, as in my case,
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.setContentLength(0);
does the trick. If Content-Length: 0 , print() will not have an effect, even if it is used, for example:
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.setContentLength(0); response.getWriter().print("this string will be ignored due to the above line");
the client gets something like:
HTTP/1.1 401 Unauthorized Server: Apache-Coyote/1.1 Content-Type: text/html;charset=utf-8 Content-Length: 0 Date: Wed, 28 Sep 2011 08:59:49 GMT
If you want to send an error message, use setContentLength() with the message length (other than zero) or you can leave it on the server
manikanta Sep 28 2018-11-11T00: 00Z
source share