title: Should I flush ServletOutputStream every time I write to it?
body: <p>I am getting following error in my AttachedFiles.jsp page:</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [jsp] in context with path [/da8] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
  java.lang.IllegalStateException: getOutputStream() has already been called for this response
      at org.apache.catalina.connector.Response.getWriter(Response.java:678)
      at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
      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.PageContextImpl.release(PageContextImpl.java:194)
      at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
      at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
      at org.apache.jsp.jsp.AttachedFiles_jsp._jspService(AttachedFiles_jsp.java:786)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
  </code></pre>
  
  <p>My previous version had:</p>
  
  <pre><code>fileIn.close();
  outStream.flush();
  outStream.close();
  </code></pre>
  
  <p>right after while loop, but I thought it is a good practice to place closing of all objects in finally block.
  I also read that close() also flushes the OutputStream.</p>
  
  <p>After I made this change I am getting above error :/</p>
  
  <p>AttachedFiles.jsp</p>
  
  <pre><code>    FileInputStream fileIn = null;
      ServletOutputStream outStream = null;
      try 
      {   
          File fileToDownload = new File(file);
  
          response.setContentType("APPLICATION/OCTET-STREAM");
          String fileName = request.getParameter("fileName");
          if(fileName == null) fileName = "";
  
          String disHeader = "Attachment; Filename=\""+fileName+"\"";
          response.setHeader("Content-Disposition", disHeader);
          fileIn = new FileInputStream(fileToDownload);
          outStream = response.getOutputStream();
  
          byte[] outputByte = new byte[(int) fileToDownload.length()];
  
          //copy binary contect to output stream
          while(fileIn.read(outputByte) != -1)
          {
              outStream.write(outputByte);
          }
      }
      catch (IOException e)
      {
          e.printStackTrace();
      }
      finally
      {
          try
          {
              outStream.close();
              if (fileIn != null)
                  fileIn.close();
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  </code></pre>
  
  <p>Does anyone know why I am getting this error?</p>
  
  <p>if so, should I flush inside while loop , right after writing to outputStream?</p>