Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Servlets: why is PrintWriter.flush() not flushing?
    primarykey
    data
    text
    <p>I am currently writing a Comet application which requires me to send chunks of data at a time on a persistent connection. However, I'm having trouble flushing the message to the client before closing the connection. Is there any reason the PrintWriter.flush() method is not behaving like I think it should?</p> <p>This is my Tomcat Comet implementation:</p> <pre><code>public void event(CometEvent event) throws IOException, ServletException { HttpServletRequest request = event.getHttpServletRequest(); HttpServletResponse response = event.getHttpServletResponse(); if (event.getEventType() == EventType.BEGIN) { request.setAttribute("org.apache.tomcat.comet.timeout", 300 * 1000); PrintWriter out = response.getWriter(); out.println("BEGIN!"); out.flush(); System.out.println("EventType.BEGIN"); } else if (event.getEventType() == EventType.READ) { InputStream is = request.getInputStream(); byte[] buf = new byte[512]; do { int n = is.read(buf); //can throw an IOException if (n &gt; 0) { System.out.println("Read " + n + " bytes: " + new String(buf, 0, n) + " for session: " + request.getSession(true).getId()); } else if (n &lt; 0) { return; } } while (is.available() &gt; 0); System.out.println("subtype: "+event.getEventSubType()); System.out.println("EventType.READ"); } else if (event.getEventType() == EventType.END) { PrintWriter out = response.getWriter(); out.println("END!"); out.close(); System.out.println("checkError: "+out.checkError()); System.out.println(event.getEventSubType()); System.out.println("EventType.END"); //eventWorker.enqueue(new EndEvent(request, response)); } else if (event.getEventType() == EventType.ERROR) { PrintWriter out = response.getWriter(); out.println("ERROR!"); out.flush(); System.out.println("checkError: "+out.checkError()); System.out.println("subtype: "+event.getEventSubType()); //response.getWriter().close(); System.out.println("EventType.ERROR"); } else { (new ServletException("EXCEPTION")).printStackTrace(); } } </code></pre> <p>So here I'm trying to send the message "BEGIN!" and keep the connection open afterwards so I can send more data. However, it seems that the message doesn't go through until the connection is closed.</p> <p>This is my ajax code: $.post('comet', function(data) { alert(data); }); </p> <p>After I run this code, Firebug tells me that this is the response header: Server: Apache-Coyote/1.1 Transfer-Encoding: chunked Date: Mon, 13 Jul 2009 21:16:29 GMT</p> <p>This leads me to think that my browser received some data back, but how do I update something on the page before the connection is closed?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload