java.lang.IllegalStateException: Already using output stream

40,396

Solution 1

You cannot use both getServletOutputStream() and getWriter() in same response.

Coming to your problem. Avoid writing scriptlets in JSP. Whatever you are doing in JSP , implement it in Servlet.

You are calling response.getOutputStream(); in JSP which is illegal. You should use either ServletResponse.getOutputStream() or ServletResponse.getWriter(). Since JSP's use ServletResponse.getWriter() by default. You should write to ServletResponse.getWriter() instead ServletResponse.getOutputStream()

This is what Java Doc says :

getOutputStream...

ServletOutputStream getOutputStream() throws IOException

Returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data.

Calling flush() on the ServletOutputStream commits the response. Either this method or getWriter() may be called to write the body, not both.

Returns: a ServletOutputStream for writing binary data Throws: IllegalStateException - if the getWriter method has been called on this response

Solution 2

Seems like the exception is thrown from this line.

out.println(e);

In case your code to send the PDF as Application fails during writing then it will thrown and exception and while trying to print the exception with the above line the outputputstream is already being used.

Do not intermix UI and Business Logic in JSP. Use a Servlet to do this job.

Share:
40,396
Vignesh Vino
Author by

Vignesh Vino

Windchill developer.

Updated on July 09, 2022

Comments

  • Vignesh Vino
    Vignesh Vino almost 2 years

    windchill GUI on client side browser when a user clicks on a button particular pdf file should gets downloaded on his system.I have achieved this by using the following code.

       <body>
        <%
        String pdfname=   session.getAttribute("pdfname").toString();
        String Pdfpath=   session.getAttribute("pdfpath").toString();
        File f =new File(Pdfpath);
         Boolean flag=false;
          if(f.exists())
          {
         BufferedInputStream filein = null;
         BufferedOutputStream out2=null;
        try {
        File file = new File(Pdfpath);//specify the file path
        byte b[] = new byte[1048576];
        int len = 0;
        filein = new BufferedInputStream(new FileInputStream(file));
        out2=new BufferedOutputStream(response.getOutputStream());
        response.setHeader("Content-Length", ""+file.length());
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition","attachment;filename="+pdfname);
        response.setHeader("Content-Transfer-Encoding", "binary");
        while ((len = filein.read(b)) > 0) {
        out2.write(b, 0, len);
        out.println("Your Pdf Document Is Generated Please close it");
        }
        filein.close();
        out2.flush();
        out2.close();
      }
        catch(Exception e)
    {
        out.println(e);
        }
    
          }else{
    
            String error ="File Not Found Or File Has Bean Deleted Already";
            request.setAttribute("error", error);
            RequestDispatcher s = request.getRequestDispatcher("NoFile.jsp");
                    s.forward(request, response);
        }
         %>
        </body>
    

    This code works fine and file is getting downloaded but after that It throws an exception.The following is my method server log

    ERROR : org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/Windchill].[jsp]  - Servlet.service() for servlet jsp threw exception
    Thu 3/28/13 12:29:07: TP-Processor7: java.lang.IllegalStateException: Already using output stream
    Thu 3/28/13 12:29:07: TP-Processor7:    at wt.servlet.CompressionFilter$GzippingResponse.getWriter(CompressionFilter.java:860)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.jasper.runtime.JspWriterImpl.flush(JspWriterImpl.java:173)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.jasper.runtime.JspWriterImpl.close(JspWriterImpl.java:187)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.jsp.netmarkets.jsp.gt.get_jsp._jspService(get_jsp.java:105)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    Thu 3/28/13 12:29:07: TP-Processor7:    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    Thu 3/28/13 12:29:07: TP-Processor7:    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
    Thu 3/28/13 12:29:07: TP-Processor7:    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    Thu 3/28/13 12:29:07: TP-Processor7: ......
    

    ......

    There are lots of post available in google regarding this exception but I can't clear my mistake. I've also tried adding this in servlet instead of jsp. That too shows the same exception.Is this way correct for downloading a file or I'm in wrong way? I need help

    Thanks

  • Vignesh Vino
    Vignesh Vino about 11 years
    I have closed </body> before response now.It shows same exception
  • mthmulders
    mthmulders about 11 years
    Why are you including a <body> and </body> at all? You're streaming a PDF, aren't you?
  • Vignesh Vino
    Vignesh Vino about 11 years
    Yes..Since I'm using jsp I thought scriptlets have to added within body tag
  • mthmulders
    mthmulders about 11 years
    If the only thing your JSP does is streaming a PDF, you don't have to surround the scriptlet with <body> and </body>.
  • Vignesh Vino
    Vignesh Vino about 11 years
    When adding the ServletResponse.getWriter() to the output stream it showing the error Cannot make a static reference to the non-static method getWriter() from the type ServletResponse Help to solve this