java.lang.IllegalStateException: Exception occurred when flushing data

10,804

My guess is that your call to response.getWriter() initiates writing the response to the client when it's being flushed. For that reason, you cannot anymore forward the request, as it has been already been flushed on.

You are allowed to set your response content type multiple times without it mattering, but you shouldn't call a writer unless you're already done with pre-processing.

Note also that calling getOutputStream() has the same effect.

Share:
10,804
Suhail Gupta
Author by

Suhail Gupta

"There's nothing more permanent than a temporary hack." - Kyle Simpson "The strength of JavaScript is that you can do anything. The weakness is that you will." - Reg Braithwaite I am on internet Twitter @suhail3 E-mail [email protected]

Updated on June 05, 2022

Comments

  • Suhail Gupta
    Suhail Gupta almost 2 years

    Possible Duplicate:
    Exception occurred when flushing data . What is this and why am I getting this?

    The following is a snippet from a filter. It gets the client IP , sets the attribute and then chains the request to a servlet.

    @Override
     public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) 
            throws ServletException,IOException {
        String IP = request.getRemoteAddr();
        request.setAttribute("client IP from the filter", IP);
        chain.doFilter(request, response);
    }
    

    The following is a snippet from the servlet which has received a filtered request. It gets the IP , stores it as an another attribute and then dispatches the request to index.jsp .

        @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String IP = (String)request.getAttribute("client IP from the filter");
        request.setAttribute("Client IP", IP);
        RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
        rd.forward(request,response);
    }
    

    index.jsp , then retrieves the attribute set by the servlet and works upon it.

    The servlet is the first thing to run when a website foo.com is first opened. But as I open the website I see this stack trace :

    java.lang.IllegalStateException: Exception occurred when flushing data
    at com.google.appengine.runtime.Request.process-d6995d0c305e239e(Request.java)
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:191)
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
    at org.apache.jasper.runtime.JspFactoryImpl.access$100(JspFactoryImpl.java:40)
    at org.apache.jasper.runtime.JspFactoryImpl$PrivilegedReleasePageContext.run(JspFactoryImpl.java:166)
    at java.security.AccessController.doPrivileged(AccessController.java:34)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:139)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
    at Servlets.FW_FirstSite.doGet(FW_FirstSite.java:27)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at Filters.FirstSiteFilter.doFilter(FirstSiteFilter.java:24)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:452)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:458)
    at com.google.tracing.TraceContext.runInContext(TraceContext.java:698)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:336)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:328)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:456)
    at java.lang.Thread.run(Thread.java:679)
    Caused by: java.io.IOException: Stream closed
    at org.apache.jasper.runtime.JspWriterImpl.ensureOpen(JspWriterImpl.java:204)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:115)
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:188)
    ... 43 more
    

    Logs suggest that exception occurs at Servlets.FW_FirstSite.doGet(FW_FirstSite.java:27) which is the statement rd.forward(request,response); and at Filters.FirstSiteFilter.doFilter(FirstSiteFilter.java:24) which is the statement chain.doFilter(request, response);.

    Why do get these exceptions ?

  • Suhail Gupta
    Suhail Gupta over 11 years
    I edited but getting the same exception
  • eis
    eis over 11 years
    @SuhailGupta and redeployed too? Please then edit the question and remove the lines, so it would be updated with all the latest code. You do have all the code where you manipulate your request and response objects posted now, right?
  • Suhail Gupta
    Suhail Gupta over 11 years
    edited the question. yes, i redeployed it too