Unable to set header in JSP. The answer is already made

WebSphere logs the warning message "SRTServletRes W WARNING: header cannot be set. Response already made" for one JSP request. I need response headers later in my code. I did some research and realized that Servlet was trying to send more data to the output stream, but the stream was already committed. I did not understand why this only happens with this particular JSP, as this servlet code is great for other JSPs. This page is not redirected, and I get the response back without response headers.

+7
java jsp servlets websphere
source share
1 answer

When the answer is complete, it means that at least the headers have already been sent to the client side. You cannot set / change the headers when the answer is already executed, because it is too late.

The response will be executed whenever one or more of the following conditions is true:

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

The 2K buffer limit is configured in the application server configuration.

You need to rebuild the code logic so that it only sets the headers before the response is executed. You should never set / change response headers using scriptlets inside / half of the JSP. You should only do this in Filter before continuing the chain or in the Servlet page controller before sending the request. Also make sure that none of them are called by the JSP file.

+20
source share

All Articles