Cannot set header. Response already committed

I found this to be happening in alot of the log files. Further investigation I found out that it happens because, part of the response is already sent to the client, including the header information, and then we were trying to manipulate that header information in some some jsp include files. Basically you cannot set/change headers when the response is already committed, because it’s too late. So we changed the logic of our pages and introduced a ServletFilter.

When a response is committed, it means that at least the headers are already been sent to the client side. You cannot set/change headers when the response is already committed, because it’s too late.

A response will be committed whenever one or more of the following conditions is met:

  • HttpServletResponse#sendRedirect() has been called.
  • More than 2K has already been written to the response output, either by Servlet or JSP.
  • More than 0K but less than 2K has been written and flush() was been invoked on the response output stream, either by Servlet or JSP.

The 2K buffer limit is configureable in appserver’s configuration.

You need to rearrange the code logic so that it only sets the headers before the response is committed. You should never set/change the response headers using scriptlets inside/halfway a JSP. You should only do that in a Filter before continuing the chain, or in a page controller Servlet before dispatching the request. Also take care that neither of them are been called by a JSP include file.

via java – SRTServletRes W WARNING: Cannot set header. Response already committed – Stack Overflow.