• 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
  • Tim Cooke
  • paul wheaton
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
  • Himai Minh
Bartenders:

Retrieving a Java Object from AJAX Respnse

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
marlajee Borstone
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSON starting : http://secretgeek.net/json_3mins.asp
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see where your going ... hmmph, makes sense.

Trilochan.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trilochan, thanks for the link and Gregg, as always, great explanation..
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic