• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AJAX with servlets

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic