Well it is unclear in the specs, but it seems to work in Tomcat 7.0.
Extract from the specs for servlet 3.0 :
Closure of Response Object
When a response is closed, the container must immediately flush all remaining
content in the response buffer to the client. The following events indicate that the
servlet has satisfied the request and that the response object is to be closed:
- The termination of the service method of the servlet.
- The amount of content specified in the setContentLength method of the response
has been greater than zero and has been written to the response.
- The sendError method is called.
- The sendRedirect method is called.
- The complete method on AsyncContext is called.
From my tests on a tomcat 7.0.32, the Http connection is closed before the end of the service
method of the servlet when ContentLength is set to 0 and the output stream is closed.
So per you requirements, you could try the following in your servlet :
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
response.setContentLength(0);
response.getOutputStream().close();
// continue after connection with client is closed
But BEWARE I could find no confirmation in the specs so it could not work with another container.