title: Servlet response buffer not flushing without newlines
body: <p>My example servlet prints out some data, flushes the buffer, sleeps for 5 seconds, then prints out more data and finishes.</p>
  
  <pre class="lang-java prettyprint-override"><code>public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
  
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    writer.println("Before sleep&lt;br/&gt;");
    writer.print("After newline&lt;br/&gt;");
    writer.flush();
    response.flushBuffer();
    try {
      Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
    writer.print("After sleep&lt;br/&gt;");
  }
  </code></pre>
  
  <p>I expect the response body leading up to the flush</p>
  
  <pre class="lang-none prettyprint-override"><code>Before sleep&lt;br/&gt;
  After newline&lt;br/&gt;
  </code></pre>
  
  <p>to be printed immediately, and 5 seconds later, the last piece.</p>
  
  <p><code>Before sleep&lt;br/&gt;</code><br/>
  <code>After newline&lt;br/&gt;<strong>After sleep&lt;br/&gt;</strong></code></p>
  
  <h2>But no</h2>
  
  <p>When I query this servlet with <code>curl -v</code>, the only immediate output is the headers and the response body leading up to (and including) the newline</p>
  
  <pre class="lang-none prettyprint-override"><code>Before sleep&lt;br /&gt;
  </code></pre>
  
  <p>with the rest printed after 5 seconds.</p>
  
  <p><code>Before sleep&lt;br/&gt;</code><br/>
  <code><strong>After newline&lt;br/&gt;After sleep&lt;br/&gt;</strong></code></p>
  
  <h2>Why?</h2>
  
  <p>It looks like the web container is line-buffering the response data. But that means the servlet can only flush response data before a newline, which seems to violate the specification.</p>
  
  <p>Does anyone know why this is happening?</p>
  
  <p>(I am using WildFly 8.2 with Java EE 7 / Servlets 3.1.)</p>