Akka Jaworek Akka Jaworek - 1 year ago 86
Java Question

http servlet request is commited, what does it mean?

As stated in title, what does it mean that Http servlet request is commited?

i have some request interceptor, extednig

, that overrides
method. Post handle method takes paramether
final HttpServletRequest response
. In method body there is an
statment checking if
, what does that exactly checks?

private static final String voidResponse = "null";

public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler,
final ModelAndView modelAndView) throws IOException {
if (!response.isCommitted()) {
if (DefaultServletHttpRequestHandler.class == handler.getClass()) {
try (final Writer writer = response.getWriter()) {
log.info("void method called, respond with 200 and null");


EDIT: of course i ment
HttpServletRequest response
, bad copy-pase
EDIT2: added code

Answer Source

ServlerResponse.isCommited() checks if the response has been already committed to the client or not (Means the servlet output stream has been opened to writing the response content).

The committed response holds the HTTP Status and Headers and you can't modify it. It's important also to note that in this case the response content has NOT been written yet, as the headers and status are committed before the content itself.

In such examples as yours the check is required to prevent situations when the response has already been commited but someone is trying to modify it, in which case you will get an IllegalStateException stating that response has already been committed.

UPDATE: I see that you are using Spring controllers. The story differs a bit here.

  • Case 1: If you are using @ResponseBody in your controller method or returning ResponseEntity Spring writes to and commits the response before the postHandle() is called, which makes it impossible to change the response later. That said in this case response.isCommited() statement will always return true and you are not able to modify the response.
  • Case 2: If you don't have the above mentioned annotation and don't return ResponseEntity or controller returns NULL the postHandle() method of interceptor is called after the controller method has been processed, but the response has not been committed yet. This means you can modify the response as you want (e.g. return 200 OK).