• 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

Replacing the contents of a div using an AJAX request made from the server-side?

 
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, I have java objects whose values will constantly change and be polled as my web-app is being ran. Whenever, the value is polled and is different from the result of the last poll, I want to do an AJAX post request(post the name of the object, the div will have an id of the same name) to a div containing a table of data about the object on a jsp page.

The div's innerHTML shall then be re-written to display the new data. Is doing this from the server java code through a servlet to the jsp possible? Up until now I've only been calling GETs and POSTs using jquery on the jsp page.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you are asking. Are you asking if a servlet can be used to service the Ajax request? If so, then yes. From the point of view of the server, it's just like any other request.

If it's something else you are asking, you'll need tone clearer regarding your question.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, you could implement a Comet model using Java technology on the serverside and JavaScript on the clientside.
This might be a useful article.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I'm not sure what you are asking. Are you asking if a servlet can be used to service the Ajax request? If so, then yes. From the point of view of the server, it's just like any other request.

If it's something else you are asking, you'll need tone clearer regarding your question.



Really sorry if I'm not making sense. What I want to know is if I could send an ajax request from java code, have the servlet handle the request and have it be sent it to the jsp, where the jsp will read the object name sent by the POST and overwrite the div that shares the name of the object with the new data. Really, the only thing I'm not sure about is how the jsp will recieve the request and what to do with it. Will it use some kind of listener? The jsp itself did not make the request so that's where I'm confused and am new to.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jelle Klap wrote:Sure, you could implement a Comet model using Java technology on the serverside and JavaScript on the clientside.
This might be a useful article.



That sounds like what I'm looking for! Thank you!
 
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

Sean Michael Hayes wrote:What I want to know is if I could send an ajax request from java code, have the servlet handle the request and have it be sent it to the jsp, where the jsp will read the object name sent by the POST and overwrite the div that shares the name of the object with the new data.



In other words, you send the request to the servlet, which handles the request and then forwards to the JSP, which generates the HTML to be put into that div? Yes, that's what you should always be doing.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Sean Michael Hayes wrote:What I want to know is if I could send an ajax request from java code, have the servlet handle the request and have it be sent it to the jsp, where the jsp will read the object name sent by the POST and overwrite the div that shares the name of the object with the new data.



In other words, you send the request to the servlet, which handles the request and then forwards to the JSP, which generates the HTML to be put into that div? Yes, that's what you should always be doing.



Apologies for the confusion but I know the term for what I want done now, I want to push in data from the web server to the browser.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jelle Klap wrote:Sure, you could implement a Comet model using Java technology on the serverside and JavaScript on the clientside.
This might be a useful article.



I'm very sorry to say but, that example, while informative on definitions, fails as an example in practice. It doesnt work. I've spent two hours trying to get it working only for it to fail, there are also numerous threads on stack overflow and one or two here about the example not working.

Is there a more clear or "layman's term" example for java out there? There seems to be little support for someone who wants to learn the basics. (Or even if this is the right thing for me, I've been looking at pushlets and websockets as well)
 
Paul Clapham
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
If you're interested in learning the basics, then stop messing about with Comet. That is far from being "the basics". Just go back to your original plan where you're going to send an AJAX request and put the results into a div somewhere. That qualifies as "the basics" these days. Just forget about the whole concept of having the webserver push data to the client and stick with the original plan to have the client poll the server.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. Technology such as Comet is really really expensive resource-wise and should only be used when it's really really needed and you have a lot of server-side muscle.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main reason I wanted to do a server push method was concerns over scalability. I could ,in theory, have hundreds of machines having their data returned even though there has been no change in their values. I just don't want unnecessary or redundant things happening in my app.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have concerns over scability, then a technology such as Comet is definitely not what you want to consider.

What's more expensive? Returning many small packets of data, perhaps needlessly. Or holding a channel open all the time whether it's going to be used or not?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, if you are concerned about the overhead of many Ajax calls, there are other ways to make quick pings to the server to get a yes/no answer (is there new data or fetch to not, for example).

Consider the following hint and see if it lights any bulbs:
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Michael Hayes wrote:The main reason I wanted to do a server push method was concerns over scalability. I could ,in theory, have hundreds of machines having their data returned even though there has been no change in their values. I just don't want unnecessary or redundant things happening in my app.



But this is just premature optimization. You have no idea how many users you're going to have or how long each of these refresh transactions are going to take or anything. (At least I don't see any suggestion that you've made any measurements.) You've just made a pre-emptive determination that it's going to be a problem.

I would certainly recommend sending a 304 response if the user's values haven't changed, so that the bandwidth usage is minimized and so on, but I wouldn't outright reject the polling approach until it was clearly a problem. Especially since the alternatives are technically much more complex and have their own problems.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul is absolutely correct. Resort to optimization if, and only if, there are demonstrable problems with the straight-forward approach.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok then. Better break out the books then!
I'll mark this as resolved.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic