aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Multiple responses to one request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Multiple responses to one request" Watch "Multiple responses to one request" New topic
Author

Multiple responses to one request

Daesung Park
Ranch Hand

Joined: Mar 22, 2007
Posts: 68
As far as I know, Ajax using XHR can handle only one response per one request. It seems to be limitation of web applications that uses Ajax.
For example, web page displays a table by querying to a server, but it takes several minutes for server to retrieve all data. So, I want to make server to responses currently retrieved data every 10 second and client handles every response. Is it possible or is there any workaround?


Daesung Park

BLOG
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This is typically done by having the client poll the server every n seconds to see if the job is done.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You may also want to explore the comet architecture.

I don't know how widely used it is or what kind of browser support there is for it but it's there.

I, personally, would go with polling AJAX calls.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

Originally posted by Daesung Park:
As far as I know, Ajax using XHR can handle only one response per one request. It seems to be limitation of web applications that uses Ajax.

This is a "limitation" of HTTP, not Ajax. Ajax and XHR can't make HTTP do anything that it was not designed to do.
[ January 16, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Daesung Park
Ranch Hand

Joined: Mar 22, 2007
Posts: 68
Originally posted by Ben Souther:
This is typically done by having the client poll the server every n seconds to see if the job is done.


Thanks for your reply.
I used client polling, but not satisfied with it. My dissatisfaction may comes from wrong design of server side. I can design 2 types of server-side as below

(a) In server side, jsp retrieves data and writes currently retrieved records to a output file which is the target of client's query.
: It is simple, but on each polling, reponse text is not delta but accumulative records.

(b) In server side, jsp is the target of client. Query string to the jsp should include client ID and query sequence number. If sequence num is 0, jsp starts retrieving data and writes them to a output file(Each ID has own output file). If it is over 0, jsp reads some records (stored in output file) corresponding to ID/sequence number and returns the records.

I am using design similar to (a).
Is (b) reasonable and the way typically used?
Could you share nice ideas on this issue?
[ January 17, 2008: Message edited by: Daesung Park ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I wrote a demo app that shows one way of doing this.
http://simple.souther.us/not-so-simple.html
Look for long running process.

It was kept as simple as possible so it refreshes the whole page instead of using AJAX but the same principals apply. It also uses a session scoped object to store the state of the long running job.
In the real world, I store the state in a database table so the user doesn't need to keep a session alive in order to keep in touch with the job.

In a nutshell, I kick off the the job in a new thread and immediately return a status pages that refreshes every n seconds. With each refresh, it checks for a 'Done' status. If not done, then I return the refreshing status page. If done then I return the resulting page, report, file, whatever...

It would be very easy to adapt this model to use AJAX and a database.
Another nice touch would be to have the app email the user once the job is complete so she can do other things while waiting for the job to finish.

Also, in the real world, (and I think this might answer your 'B' scenerio),
the refreshing page has a list of all jobs -- finished and unfinished -- by this user so she has the option of viewing previous reports generated with this component.

Does this help?
Daesung Park
Ranch Hand

Joined: Mar 22, 2007
Posts: 68
Thanks Ben for your kind answers.
SaurabhSri Sri
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi Daesung,

Did you implemented any solution. I am also looking for the same.
I want to make server to responses currently retrieved data every 10 second and client handles every response.

Thanks in advance.


Regards
SaurabhSri (SCJP 1.5)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple responses to one request
 
Similar Threads
Does the browser have to be AJAX aware?
How to run a method in JSP
Ajax - innerHTML
Observer pattern managedBean?
client could not get update session data