22

What are the common possibilities to encounter this exception in servlet - Response Already committed?

Sriram
  • 2,909
  • 8
  • 27
  • 35

1 Answers1

36

The response gets committed because of the following reasons:

  • Because the Response buffer has reached the max buffer size. It could be because of the following reasons:

      > the bufferSize in JSP page has reached.You can increase the JSP buffer size 
        in page directive. See here, 
    
       <%@ page buffer="5kb" autoFlush="false" %>
    
      > the server default response max buffer size has reached.You can increase    
        the server default max buffer size.
    
        ServletRespnse.setBufferSize()
    
  • Some part of the code has called flushed on the response , i,e, invoked the method HttpServletResponse.flushBuffer().

  • Some part of the code has flushed the OutputStream or Writer, i,e, invoked the method HttpServletResponse.getOutputStream().flush() or `HttpServletResponse.getWriter().flush()

  • If you have forwarded to another page, where the response is both committed and closed. For example, when response.sendRedirect() has been called, the response is committed.

Mayank Gupta
  • 31
  • 10
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
  • Thanks for the reply. Deliberately we never call any of these. But, flush="true" would cause this? So, what are the measures to consider to avoid this? – Sriram Jul 03 '12 at 06:45
  • 2
    Increase the buffer size of the jsp. – Ramesh PVK Jul 03 '12 at 06:52
  • How to do this? Can you explain with some example? – Sriram Jul 03 '12 at 07:22
  • You are having JSP or servlet? – Ramesh PVK Jul 03 '12 at 07:25
  • I actually would like to know the exact cause of this. This occurs both in servlet / jsp. You primarily focused on buffer size. Are there any other points to consider to avoid this. Anyways, your reply certainly helps me. Thanks. – Sriram Jul 03 '12 at 07:35
  • This can happen in several cases. Have listed all the cases. Added one more case as well. – Ramesh PVK Jul 03 '12 at 07:41
  • 1
    What you mean by this _the response is both committed and closed_ ? – Sriram Jul 03 '12 at 08:33
  • 9
    Committed means just writing headers. Close means writing headers + writing response + close stream. Such that you cannot write any more content. – Ramesh PVK Jul 03 '12 at 09:22
  • Check this thread (http://stackoverflow.com/questions/34292303/why-i-can-still-send-data-after-the-response-is-commited-with-response-flushbuf). You can still write to client **even after** you call `HttpServletResponse.flushBuffer()`. – smwikipedia Dec 15 '15 at 14:58
  • Yes, Sending data is different from committing !! Committing data is just writing headers. In my discussion above committing data means writing headers. – Ramesh PVK Dec 15 '15 at 21:38