This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

JQuery In action - Data Access Options

 
Ranch Hand
Posts: 439
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My understanding that is that AJAX queries can only return data via documents - either xml or json .
Is this the case or can you use jQuery or some other javascript framework to actually work with a (jdbc based) result set directly?

Thanks,
Paul
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaScript works on the client browser. JDBC is a server side technology. They cannot communicate directly.
 
Marshal
Posts: 67362
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

paul nisset wrote:My understanding that is that AJAX queries can only return data via documents - either xml or json .


Your understanding is not correct. An Ajax request is no different than any other HTTP request -- in other words, the response can be any text format. That could be JSON or XML, but it could also be an HTML fragment (very popular with the jQuery .load() method), plain text, or any other text-based data.

Is this the case or can you use jQuery or some other javascript framework to actually work with a (jdbc based) result set directly?


jQuery can't make HTTP to do something for which it is not intended. Your best best for dealing with database data is to collect it from the result set into "normal" collections (List of beans, for example) and serialize it to JSON. jQuery can automatically deserialize the JSON into the JavaScript equivalent.


 
paul nisset
Ranch Hand
Posts: 439
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your responses.
I've used AJAX to call a servlets and returned formatted data with html tags. I guess this is what you are talking about with HTML fragments. I did it that way because it was simple and avoided parsing the response in the client code but I am wondering if this is bad way to do things.
The serializing beans and deserializing via jquery sounds interesting.
 
Bear Bibeault
Marshal
Posts: 67362
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

paul nisset wrote:I guess this is what you are talking about with HTML fragments. I did it that way because it was simple and avoided parsing the response in the client code but I am wondering if this is bad way to do things.


Yes that is what I meant, and no, it's not in the least a poor practice. It's a very common and supported thing to do.

The serializing beans and deserializing via jquery sounds interesting.


When you need to ferry data to the client side, it's invaluable.

That said, I try to avoid sending data when formatting an HTML fragment on the server will do.

For example, when creating cascading dropdowns, you could send the name/value pairs for the options as a serialized Map, and then write script to create the <option> elements. But I prefer to render the <option> tags in a JSP and just inject them into the dropdown as an HTML fragment.
 
paul nisset
Ranch Hand
Posts: 439
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What sort of situation would recommend using the serialized bean approach?
 
Bear Bibeault
Marshal
Posts: 67362
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you need the data for some reason other than just building HTML fragments using it.
 
paul nisset
Ranch Hand
Posts: 439
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input.
 
Bear Bibeault
Marshal
Posts: 67362
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One example I can think of is form validation. When I submit a form from a dialog, I almost always submit it via Ajax so that, in the event of failure, I don't have to try and recreate the form and its contents.

I'll return a construct describing any validation errors. If none, I dismiss the dialog. If there are failures, the data decribes which fields failed and why, which I use to put up messages and change the class of the fields to visually indicate that they've failed. Because all the HTML for the form already exists, and I need information on precisely which fields failed, returning an HTML fragment isn't appropriate.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!