This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes async JSP and servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "async JSP and servlet" Watch "async JSP and servlet" New topic
Author

async JSP and servlet

Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
First, I'm using jquery $.ajax. I'm wondering how to send ajax request to servlet every page load, and receive object (not json text) from servlet. what's the code in the servlet side and what's inside the success callback?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Where's your code? What problems are you having with it? You should know by now that no one here is just going to do your work for you.

Hendra Kurniawan wrote:First, I'm using jquery $.ajax. I'm wondering how to send ajax request to servlet every page load,

If you're going to fetch on every page load, why bother with Ajax and why not just do whatever it is your doing on the server before the page is sent to the browser?

and receive object (not json text) from servlet.

You can't send objects in HTTP, you can only send text.

Again, if it's not text you need, why the Ajax? Why not deal with this on the server?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
I need ajax to avoid page refresh. after some thoughts, it wouldn't make sense to place ajax call every page refresh. you got a point. But I just want to clarify, it's impossible to send object via ajax? absolutely impossible? text only? if so, does java have built-in lib to convert object to json?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

There's no page refresh involved with pre-formating the page with JSP (or whatever you are using on the server).

After the page has been loaded, Ajax is used to avoid page reloads after user clicks and so on. But on page load, it's usually better to take of formatting the page on the server before it's sent to the client in the first place.
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
and how about the json lib? java has built-in? or must use third party? if third party, what's your recommendation? thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Well that's a bit of a non sequitor. But there are quite a number of fine JSON libraries available, but for various reasons I mostly use Google's Gson.
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
GSON? java doesn't have built-in lib for this purpose?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, it doesn't.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: async JSP and servlet