Meaningless Drivel is fun!
The moose likes Servlets and the fly likes AJAX with servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "AJAX with servlets" Watch "AJAX with servlets" New topic

AJAX with servlets

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750

I have got AJAX to work with servlets, the servlet code is:

and in the jsp page, I basically have...

This works fine, but what I want to do is get servlet to send back some data, then pause for 5 seconds, then send back another piece of data.

Is this possible, thanks for any help.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Why would you want to do that? Given that the servlet can only send one response, and that it's going to be buffered in transmission, it sounds rather iffy to me. Why not just have your Ajax code send one request for the first bit, then 5 seconds later send another request for the second bit?

Edit: or you could have the servlet send all of the data, and let your Ajax code pick out the bit which is supposed to be displayed 5 seconds later and display it 5 seconds later.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
Well, basically I wanted to get one client to be able to update a second clients page.
So my idea was for one client to store data as a ServletContextAttribute, then use a listener to 'try to' send data to the other client.

But now that you said that, I could get a client to repeatadly call the server, (perhaps using that javascript setTimeout(...) function), so that it calls servlet, which returns any new Context attributes.

What do you think?

Arvind Subramanian
Ranch Hand

Joined: Jul 25, 2008
Posts: 84
yeah your idea is correct and i too implemented this in my portal.
just call the ajax function with settimeout javascript fuction. so that the ajax request will be made every given interval and new data will be received from the server.

javascript code

"Many of lifes failure are people who did not realize how close they were to success when they gave up."
-Thomas Edison
I agree. Here's the link:
subject: AJAX with servlets
It's not a secret anymore!