title: I m getting java.lang.IllegalStateException: getOutputStream() has already been called for this response Exception
body: <p>java.lang.IllegalStateException: getOutputStream() has already been called for this response</p>
  
  <p>Hi I'm getting above exception while m trying to open a pdf file through my java code</p>
  
  <p>Here is my Java code. I am using IText to create pdf </p>
  
  <pre class="lang-java prettyprint-override"><code>@RequestMapping(value = "/pdf", method = RequestMethod.POST)
  public String pdf(ActivitiesForm activitiesForm, Model model,
          HttpSession session,HttpServletResponse response) throws InstanceNotFoundException, IOException,
          DocumentException {
  
      String PATH = "/home/space/workspace/trainning/Trainning/Performance.pdf";
      // step 1
      Document document = new Document();
      // step 2
       FileOutputStream fos = new FileOutputStream(PATH);
      PdfWriter.getInstance(document,fos);
      // step 3
      document.open();
      // step 4
      PdfPTable table;
  
      table = new PdfPTable(3);
      table.setWidthPercentage(100);
      PdfPCell cell;
  
      cell = new PdfPCell(new Phrase("Roll No"));
      cell.setColspan(1);
      // cell.setBorder(Rectangle.NO_BORDER);
      table.addCell(cell);
      cell = new PdfPCell(new Phrase("Name"));
      cell.setColspan(1);
      // cell.setBorder(Rectangle.NO_BORDER);
      table.addCell(cell);
      cell = new PdfPCell(new Phrase("Performance"));
      cell.setColspan(1);
      // cell.setBorder(Rectangle.NO_BORDER);
      table.addCell(cell);
  
      Integer count = rollService.getRollCount();
      Integer countpts = creditptsService.getPtsCount();
  
      if (countpts != null) {
          for (Integer i = 1; i &lt;= count; i++) {
              Roll roll = rollService.getRollNoById(Long.parseLong(i
                      .toString()));
  
              List&lt;Creditpts&gt; creditpts = creditptsService.getRollById(roll
                      .getId());
  
              Integer performance = 0;
  
              if (creditpts == null) {
  
                  cell = new PdfPCell(new Phrase("" + roll.getRollno()));
                  cell.setColspan(1);
                  // cell.setBorder(Rectangle.NO_BORDER);
                  table.addCell(cell);
  
                  cell = new PdfPCell(new Phrase("" + roll.getStudentName()));
                  cell.setColspan(1);
                  // cell.setBorder(Rectangle.NO_BORDER);
                  table.addCell(cell);
  
                  cell = new PdfPCell(new Phrase("0"));
                  cell.setColspan(1);
                  // cell.setBorder(Rectangle.NO_BORDER);
                  table.addCell(cell);
  
              } else {
                  Iterator&lt;Creditpts&gt; creIterator = creditpts.iterator();
  
                  while (creIterator.hasNext()) {
                      Creditpts creditpt = creIterator.next();
  
                      performance += creditpt.getCrdpts();
                  }
  
                  cell = new PdfPCell(new Phrase("" + roll.getRollno()));
                  cell.setColspan(1);
                  // cell.setBorder(Rectangle.NO_BORDER);
                  table.addCell(cell);
  
                  cell = new PdfPCell(new Phrase("" + roll.getStudentName()));
                  cell.setColspan(1);
                  // cell.setBorder(Rectangle.NO_BORDER);
                  table.addCell(cell);
  
                  cell = new PdfPCell(new Phrase("" + performance));
                  cell.setColspan(1);
                  // cell.setBorder(Rectangle.NO_BORDER);
                  table.addCell(cell);
  
              }
          }
      }
      document.add(table);
      document.close();
  
      fos.close();
      FileInputStream baos = new FileInputStream(PATH);
  
      // set some response headers
      response.setHeader("Expires", "0");
      response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
      response.setHeader("Pragma", "public");
      response.setContentType("application/pdf");
  
      response.setContentLength((int) new File(PATH).length());
      OutputStream os = response.getOutputStream();
      byte buffer[] = new byte[8192];
      int bytesRead, i;
  
      while ((bytesRead = baos.read(buffer)) != -1) {
          os.write(buffer, 0, bytesRead);
      }
      os.flush();
      os.close();
  }
  </code></pre>
  
  <p>I have paste my pdf creation and opening code
  what i'm doing wrong here m i missing some thing...</p>
  
  <p>Here is full stack trace:</p>
  
  <pre><code>java.lang.IllegalStateException: getOutputStream() has already been called for this response
  at org.apache.catalina.connector.Response.getWriter(Response.java:633)
  at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
  at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
  at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
  at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
  at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:336)
  at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:353)
  at org.apache.jsp.jsp.activity_jsp._jspService(activity_jsp.java:402)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
  at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
  at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
  at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:229)
  at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
  at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
  at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
  at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:792)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
  at java.lang.Thread.run(Thread.java:722)
  </code></pre>