title: Servlet mixes header and content and writing twice same in output?
body: <p>I have implemented servlet which behaves not stable, sometimes it mixes header in content and writing same twice. </p>
  
  <p>and sometimes it is returning file which contains response header mixed by content like this:</p>
  
  <pre><code>Server: Apache-Coyote/1.1
  : W/"43-1353687036000"
  DatCCoonntenntt--DDiissppoosittiioonn: : atatatacehnmte;n tf;i lfenlaemnea=m20=12201112211127325421_4W1_Wirnkgi_nSgc_Seern.xnlsx
  sx
  Content-Typ-eT: ype: applaipcatciaoti/on/toctestt-rstare
  am
  ConCtoententy-pTeype: appalicatcion/oon/octet-setarm
  m
  CCoonntent-Lnegtht h: 4199
  
  Date: te: FriF,r i2,3  2No vNo2v0 120162: 215:25 :G4M2T 
  ....
  File content bytes ...
  
  And again same header and content
  </code></pre>
  
  <p><strong>UPDATE</strong>
  *<em>This situation happens on Tomcat7</em>*</p>
  
  <p><strong>I have tested also on Tomcat6 and Jetty, in both cases there is no injection of HTTP-Header to response content but HTTP-Header is wrong and returns wrong file name, the file content is correct file. I have noticed that wrong return from servlet happens when 
  returns transfer-encoding is chunked.</strong> </p>
  
  <p>When I am removing header stuff, and second part of bytes, it is valid file. 
  Is it possible that is synchronization issue ?</p>
  
  <p><strong>UPDATE</strong>
  Here is full source of servlet :</p>
  
  <pre><code>public class ExcelDownloadServlet extends HttpServlet
  {
      private static final long serialVersionUID = 1L;
      private static final Logger LOG = Logger
              .getLogger (ExcelDownloadServlet.class);
  
  
      @Override
      protected void doGet (HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException
      {
          try
          {
              TransactionId transactionId = getTransactionId (request);
              String fileName =
                      request.getParameter (GlobalConstants.EXCEL_FILE);
              ExcelDownloadType downloadType =
                      ExcelDownloadType
                              .valueOf (request
                                      .getParameter (GlobalConstants.EXCEL_DOWNLOAD_TYPE));
              ActionContextFactory actionContextFactory =
                      ApplicationContext.getContext ()
                              .getActionContextFactory ();
              //suppress warning. HttpServletRequest.getLocales does not support generics
              @SuppressWarnings("unchecked")
              ActionContext actionContext =
                      actionContextFactory.create (request.getSession ()
                              .getId (), Collections.&lt;Locale&gt; list (request
                              .getLocales ()));
              GetExcelDataResponse dataResponse =
                      new GetExcelData (transactionId, fileName, downloadType)
                              .execute (actionContext);
              writeToResponse (response, dataResponse.getFileName (),
                      dataResponse.getData ());
          }
          catch (InvalidSessionException e)
          {
              LOG.error ("Invalid session in Excel download", e);
              throw new ServletException (e);
          }
          catch (ActionException e)
          {
              LOG.error ("Could not download into excel.", e);
              throw new ServletException (e);
          }
      }
  
      protected TransactionId getTransactionId (HttpServletRequest request)
      {
          return RequestParameterDeserializer.&lt;TransactionId&gt; deserialize (
                  request, GlobalConstants.TRANSACTION_ID);
      }
  
      protected void writeToResponse (HttpServletResponse response,
              String rawFileName, byte[] data) throws IOException
      {
          ServletOutputStream sout = null;
          try
          {            
              response.setContentType ("application/octet-stream");
              response.setContentLength (data.length);
              // removing blanks from the file name, since FF cuts file names
              // otherwise.
              String fileNameWithTime = rawFileName.replaceAll (" ", "_");
              response.setHeader ("Content-Disposition", "attachment; filename="
                      + fileNameWithTime);
              sout = response.getOutputStream ();
              sout.write (data, 0, data.length);
          }
          finally
          {
              if (sout != null)
              {
                  sout.close ();
              }
          }
      }
  </code></pre>
  
  <p><strong>UPDATE</strong>
  *<em>The call comes from GWT application when is generating the URL of servlet with required parameters and sets in IFrame, then servlet calls and file is downloading. Are there any suggestions ?</em>* </p>