Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 283235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:21:22+00:00 2026-05-12T05:21:22+00:00

I am currently writing a Comet application which requires me to send chunks of

  • 0

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?

This is my Tomcat Comet implementation:

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 > 0) {
                System.out.println("Read " + n + " bytes: " + new String(buf, 0, n) 
                        + " for session: " + request.getSession(true).getId());
            } else if (n < 0) {

                return;
            }
        } while (is.available() > 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();
    }
}

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.

This is my ajax code:
$.post(‘comet’, function(data) { alert(data); });

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:21:22+00:00Added an answer on May 12, 2026 at 5:21 am

    So it seems that the browser was receiving data this whole time, but because the connection wasn’t closed, the JavaScript thought data was still being sent. This is why my jQuery callback function was not being called.

    Looking at the W3C AJAX tutorial, I noticed that there are different ready states for the XMLHttpRequest object.

    var xmlhttp;
        if (window.XMLHttpRequest)
          {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else if (window.ActiveXObject)
          {
          // code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        else
          {
          alert("Your browser does not support XMLHTTP!");
          }
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState==3) {
                alert('process '+xmlhttp.responseText);
            }
            if(xmlhttp.readyState==4) {
                alert('ready '+xmlhttp.responseText);
            }
        }
        xmlhttp.open("GET","comet",true);
        xmlhttp.send(null);
    

    Traditionally, people only handle the 4th readyState, which means transfer is complete. In my case, however, I needed to read data before the transfer ends. Hence, I needed code to handle the 3rd readyState.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing an interface to allow applications to send exception data to a
I am writing an iPhone application which requires the user to enter several values
We are currently writing an application which is split into multiple projects/modules. For example,
I'm currently writing a TYPO3 extension which is configured with a list of tt_content
I am writing an application which tests email addresses, for one part of this
I'm currently writing an application that works with modules. The module part is all
Summary I'm currently writing an application where I have located my SQL instructions into
I'm currently writing an application in ruby on rails that uses AWS. I see
Im currently writing my bachelor thesis with latex and using TexnicCenter. I want to
Our company is currently writing a GUI automation testing tool for compact framework applications.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.