data received from a running servlet

55 views
Skip to first unread message

Akis

unread,
Jun 21, 2012, 12:36:49 PM6/21/12
to Google Web Toolkit, akismi...@gmail.com
Hi there. I've got a question:
i need to have a sort of "percentage progress" of the elaboration of
the servlet i call sending it data from a form. How could I do it?

thank you!

Joseph Lust

unread,
Jun 21, 2012, 3:00:26 PM6/21/12
to google-we...@googlegroups.com, akismi...@gmail.com
  1. Start your long running request (from client), running on a separate thread in the server, and return a unique id for that process.
  2. On the server, persist the state of that request using some Id. (do this every X seconds).
  3. On the client, make a request to poll (2) every X seconds for the status of the process (use the Id from (1)).
  4. Update something on your UI to show the % completion from (3).
  5. Once polling has shown that the long running process is complete, make a final request to get the result (payload).
Sounds like a lot, but that's how we've been doing it for a while. Of course there are many technologies you can use to do it from XHR, to WebSockets, to Comet/Channels/Errai.

Sincerely,
Joseph

Thomas Broyer

unread,
Jun 22, 2012, 4:33:46 AM6/22/12
to google-we...@googlegroups.com, akismi...@gmail.com
If you don't *have* to support IE [1], then use XMLHttpRequest progress events [2] (and possibly use FormData [3] to grab values out of a FormPanel, so that you can keep backwards compatibility and compatibility with IE, just without the progress data)

You'll have to use JSNI, as GWT doesn't expose those features, but it's doable (we did it), and IMO it's worth it (compared to the more largely cross-browser but so much more complex "hack" that Joseph proposed)

Thad

unread,
Aug 16, 2012, 4:47:01 PM8/16/12
to google-we...@googlegroups.com, akismi...@gmail.com
I check file upload progress with RequestBuilder calls:

public class UploadTimer extends Timer {

  private final RequestCallback uploaderCallback = new RequestCallback() {

    @Override
    public void onResponseReceived(Request request, Response response) {
      String text = response.getText();
      ...// update progress visual
    }

    @Override
    public void onError(Request request, Throwable exception) {
      ;
    }

  };

  RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET,
      GWT.getModuleBaseURL() + "uploadInput?check=1");

  @Override
  public void run() {
    try {
      requestBuilder.sendRequest(null, uploaderCallback);
    }
    catch (RequestException ex) {
      GWT.log(ex.getMessage());
    }
  }

}

The servlet uploadInput on POST uploads files using Apache Commons FileUpload. On GET, it uses the "Watching progress" technique described at http://commons.apache.org/fileupload/using.html and tells me the percent complete

I've a class member, private UploadTimer uploadTimer;

In my form's onSubmit(), 

    uploadTimer = new UploadTimer();
    uploadTimer.scheduleRepeating(500);
    ...// create progress visual

In my form's onSubmitComplete(),

    if (uploadTimer != null) {
      uploadTimer.cancel();
      uploadTimer = null;
      ...// hide progress visual
    }
 
Reply all
Reply to author
Forward
0 new messages