views:

1656

answers:

1

WebSphere logs the warning message “SRTServletRes W WARNING: Cannot set header. Response already committed” for one JSP request. I need the respone headers later in my code. I did some research and understood that Servlet is trying to send more data to the output stream, but the stream is already been committed. I did not understand why this is happening to only this particular JSP, as this Servlet code works fine for other JSPs. This page is not redirected and I get the response back with no response headers.

+2  A: 

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.

BalusC