That works as specified:
Sends an error response to the client using the specified status and clears the buffer. The server defaults to creating the response to look like an HTML-formatted server error page containing the specified message, setting the content type to "text/html".
HttpServletResponse.sendError(int)
on the other hand does not have this restriction/feature.
Sends an error response to the client using the specified status code and clearing the buffer.
This means: set the content type, write to the buffer, call sendError(400)
.