0

I am thinking of how to make AJAX take use of servlet 3 async response. in the request-response synchronize processing model, when the response comes back, the callback of XmlHttpRequest can get the response text; but how about the response is processed in another thread, and returns some message, what will the XHR get when the request ends? can it still get the response body? I tried a simple codes to test it, it seems failed to get the response;

I can understand this, when the AJAX request return, there is nothing in the response, it will be delayed in another server thread, so the callback get nothing.

But I wonder is there any way to let AJAX get the correct response?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
user1484819
  • 899
  • 1
  • 7
  • 18
  • Look here http://stackoverflow.com/questions/2162783/asynchronous-servlets-vs-synchronous-servlets for more information. – Mahesh Guruswamy Apr 01 '13 at 15:29
  • I read through that thread, it seems irrelevant with my question. I actually want to use AJAX in the client side, while use servlet 3 async processing in the server side. But how to get the response from the server after the processing is done? is it possible? – user1484819 Apr 02 '13 at 07:05
  • the article does talk about it. The key is persistent http connections. Here is a tutorial that talk about it http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/async-servlet/async-servlets.html – Mahesh Guruswamy Apr 02 '13 at 12:08
  • Thanks, it is what I want. however, after reading the article, I feel servlet 3 async processing makes the AJAX processing more complex. But it does work. – user1484819 Apr 02 '13 at 12:39
  • I am not sure if it makes it any more complicated than what it is right now. Obviously it is not the solution for everything. I would use it if I know that my AJAX call will take a while to respond back (long running stored proc or something like that). This way, I don't lock up that thread and I use the persistent HTTP connection to get the response back. – Mahesh Guruswamy Apr 02 '13 at 12:49

1 Answers1

0

I am afraid I made a mistake before, that I forget to call asycContext.complete() after the async processing is done. after complete() is called, ajax get the response. However, if the processing lasts longer than the timeout setting, just like following, a exception saying illegal state of the asynccontext will arise, and client get nothing:

    final AsyncContext ac = request.startAsync();


   ac.setTimeout(1000);


    Executors.newSingleThreadExecutor().execute(new Runnable(){

        @Override
        public void run() {
            PrintWriter pw;
            try {
                Thread.sleep(2000);
                pw = ac.getResponse().getWriter();
                pw.write("Hello, World!");
                ac.complete();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    });

So I guess the keys here are: 1. call complete after processing is done; 2. set a appropriate timeout;

user1484819
  • 899
  • 1
  • 7
  • 18