aspose file tools*
The moose likes Servlets and the fly likes Replacing the contents of a div using an AJAX request made from the server-side? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Replacing the contents of a div using an AJAX request made from the server-side?" Watch "Replacing the contents of a div using an AJAX request made from the server-side?" New topic
Author

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

Sean Michael Hayes
Ranch Hand

Joined: Feb 08, 2012
Posts: 54

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7

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


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Sean Michael Hayes
Ranch Hand

Joined: Feb 08, 2012
Posts: 54

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

Joined: Feb 08, 2012
Posts: 54

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!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

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

Joined: Feb 08, 2012
Posts: 54

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

Joined: Feb 08, 2012
Posts: 54

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
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

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

Joined: Feb 08, 2012
Posts: 54

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

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
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

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

Joined: Feb 08, 2012
Posts: 54

Ok then. Better break out the books then!
I'll mark this as resolved.
 
wood burning stoves
 
subject: Replacing the contents of a div using an AJAX request made from the server-side?