1 Reply Latest reply on May 25, 2007 1:57 AM by Joan Horta Tosas

    IllegalStateException: getOutputStream() has already been ca

    SR Newbie

      Hi all,

      I know many of you after reading the subject would be thinking of suggesting few changes like "don't you use getOutputStream() in JSP or use PrintWriter's getWriter instead". But I have tried most of the suggestions/pointers available in other forums.

      Problem is that it is *must* for us to use response's OutputStream object as we have to invoke a 3rd Party library API that *only* takes OutputStream object.

      Now, my concern is how can we stop JBoss calling releasePageContext during JSP lifecycle. As this also calls getOutputStream() which seems to be cause of the problem.

      finally {
      if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
      }

      Please correct me if wrong. Below is the error snippet.



      02:09:54,045 ERROR [[action]] Servlet.service() for servlet action threw exception
      java.lang.IllegalStateException: getOutputStream() has already been called for this response
       at org.apache.catalina.connector.Response.getWriter(Response.java:599)
       at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
       at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:111)
       at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
       at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:117)
       at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:182)
       at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:115)
       at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
       at org.apache.jsp.ui.rawReport_jsp._jspService(rawReport_jsp.java:873)
       at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)


      Thanks and regards,

        • 1. Re: IllegalStateException: getOutputStream() has already bee
          Joan Horta  Tosas Novice

          You can try using a Filter for getting the response output stream, doing the things with the API and putting the result in the request, then wrap the response with HttpServletResponseWrapper(response) before passing to chain.doFilter(request, wrapper). Maybe it's not the solution you expect... If not using a filter, i can't remember if first calling a servlet that does this and then forwarding to the jsp with the result would work too...
          But for sure, you can't do it inside the JSP