aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Retrieving a Java Object from AJAX Respnse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Retrieving a Java Object from AJAX Respnse" Watch "Retrieving a Java Object from AJAX Respnse" New topic
Author

Retrieving a Java Object from AJAX Respnse

marlajee Borstone
Ranch Hand

Joined: Jun 26, 2008
Posts: 35
Hi friends,
I am using Ajax in my web project. The requirement is to return lots of data from mulitple tables in database, to UI (jsp) page. I have multiple Java classeses with getter and setter methods to hold those data. Is there any way that I can pass these entire Java object itself to JSP page using AJAX. So, that on JSP page I can retrieve the values from using getter method of those objects . Storing those bulk data in xml element of XML response document and retriving back them on JSP and then adjustung for proper display is very painful .

Can someone please suggest somw easiest way to get Java object on UI page using AJAX ?

thanks in advance,
DM.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Unfortunately you can't send a Java object to the JS script, the better way is to construct a JSON object for each bean, and then process it on server side.

HTH


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
marlajee Borstone
Ranch Hand

Joined: Jun 26, 2008
Posts: 35
Hi thanks for response.
But unfortunately I don't have any idea if JSON.
Will it be recomendable to use Castor tool which will marshal and unmarshall the XML document ? Will it reduce my burden of creating XML document for Ajax response and further retrieving from xml elements ?

please suggest.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

marlajee Borstone wrote:But unfortunately I don't have any idea if JSON.


Then you should do some research on JSON. For your needs, this is really going to be the optimal solution both in terms of ease and bandwidth. It is very easy to convert Java objects into JSON and then JSON is already supported in JavaScript (It is JavaScript Object Notation, after all). Do a google for Java JSON and you'll find a ton of information.

Are you using a JavaScript library like JQuery or Prototype? If you aren't, you should also look into these for handling your Ajax requests. I recommend JQuery, but you should investigate your options and make an informed decision.


GenRocket - Experts at Building Test Data
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

I never find a good reason for using XML response instead of JSON response. All people I met said, "Look, its XML, a widely accepted markup language and its seamlessly get integrated if you're dealing with HTML and DOM things.."

But I stick to the JSON, after all its easy to use and straight forward.
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
JSON starting : http://secretgeek.net/json_3mins.asp
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
To OP: other than JSON, there are two possibilities,
1.) You can look at Rhino Engine (Could be helpful to script java, but i doubt if this is 'exactly' what you are looking for),
2.) Use JAXB on server end and parse the XML to create an object literal out here?

On JSON,
JSON vs XML debate

this quote to me works the best:

Isn’t the “JSON vs XML” debate a bit like a “PNG vs JPG” debate, in which developers of image-display applications decide their apps will handle one or the other?

In a services-oriented world, once you’re past same-site restrictions (via signed scripts or trusted zones), you will have occasion to consume services providing SOAP, POX and JSON data, based on the service-providers preference regardless of your’s. Sure, if you control the service side, JSON can be much more efficient; but existing Web services won’t add this to their APIs just to make your life easier.

IMHO, the smart path is implementing a framework that can handle various data formats, via various retrieval methods (SOAP, REST, HTTP-POST, etc.) — possibly marshalling everything to JSON for faster processing.


I like JSON, but there are organizations that already have a lot of things going on for them with XML, you can't just argue with those people because its either:
1.) Elegant
2.) More "bandwidth-friendly" <-- I don't even know if this is true, I'd like to see performance benchmarks on JSON vs XML with same datasets.
3.) Makes life easier.

I know that there a lot people who have had issues with parsing and processing XML being pain in the butt, but there are frameworks out there that make your life easier if thats a concern;


Trilochan.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Trilochan, while most of what you said is probably true in general cases, in this case I can't agree. For dealing with AJAX, assuming you have control of the server side (which the OP does), there should be no doubt what data type is returned. It should be JSON or HTML (for those cases when you just need to update a div with some content). Yes, when dealing with web services in general handling different data types is a great notion. With regards to JSON being more bandwidth friendly, you don't need benchmarks for this. You simply need to use common sense. JSON can represent the same data as XML using fewer characters. Fewer characters means less bandwidth.

And for the record, most javascript libraries like JQuery do handle different data types. But JSON is by far the simplest to process because it is already JavaScript.
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
I see where your going ... hmmph, makes sense.

Trilochan.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Trilochan, thanks for the link and Gregg, as always, great explanation..
 
Consider Paul's rocket mass heater.
 
subject: Retrieving a Java Object from AJAX Respnse