| Author |
Getting data from database using ajax
|
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
I am creating a web application using html/css/js/ajax, servlet,jsp and hibernate.
I have a method which returns Person's object, (containing properties like id, name, address, contacts). These properties has the value fetched from database.
Now, when the user enters the id in html page, i need to get his data like name , address, contacts , or rather a person object with the id entered.
For that i am using ajax.
here is the code :
Now, i don't know how to proceed.
If someone can provide a link of complete example where user gets the data from db in ajax, and sets their value in respective textfields
|
kunal
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Well, first of all your method which returns a Person (?) object is a Java method which runs on the server, isn't that correct?
So to send that Person object back to your AJAX code which sent the request, you'll need to convert it to something which can be sent as an HTTP response. That means text. You might choose JSON or XML or some other text format (but I would not invent my own wheel in this case).
Then you'll need to extract the information from that text in your Javascript code and deal with it according to your requirements.
And I would not recommend writing your own AJAX-processing code in the year 2012. Use jQuery to deal with all of the petty details and concentrate on writing code which deals with Persons and so on.
You should be able to find tutorials about AJAX and DHTML online fairly easily. But don't expect them to deal with getting data from databases because Javascript doesn't do that. If you want more extensive examples, I have "Ajax In Practice" on my bookshelf and I found it to be a good survey of the techniques you're asking about.
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
Thanks Paul for your reply
But, i am very confused now. Shall i go for hitting the server (refreshing the page) and then load the data or JQuery? AJax?
I am unknown to JQUERY, so, with jquery can i query the database and get the details and put it into file (jsp, much better than html)??
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
|
No, Ajax and jQuery (used to make Ajax easy) simply create a request to the server. Returning JSON is the easiest way to get info back to the page.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
Thanks Bear.
That means whenever i return the object to ajax code, i will go for JSON. It will fetch the data, and put it in textfields. (purpose behind putting it into textfields is to give user to edit the details)
Am i right?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
No, it will fetch the data and give it to your code to do whatever you want with it. If the data is to be used to populate text fields, that's up to you.
Using jQuery, the $.getJson() method is very useful for this.
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
|
I am not aware of jquery and json. So, if you can suggest any book or site for that
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
I believe I did exactly that in an earlier post in this thread.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
Kunal Lakhani wrote:I am not aware of jquery and json. So, if you can suggest any book or site for that
My book: jQuery in Action
Chapter 8 is all about Ajax.
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
|
Okay. Thanks Paul, Bear. Will go for Jquery in Action and jquery with Ajax
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
Hello
I am going through jQuery. Saw the examples on jquery on ajax. My class has a function which returns Person's object. Still, wonder, how can i get that person's object inside ajax code and use it getters to get the values?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
|
An Ajax request is like any other. It sends a request and expects a response. That response can be a JSON serialization of your data.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Kunal Lakhani wrote:how can i get that person's object inside ajax code and use it getters to get the values?
Remember that the Java object is not passed back to the Ajax caller. You are using HTTP so it must be converted to text first. Hence why you might convert it to JSON format.
And then remember that the JSON data is being received by Javascript code. If you were thinking that at this point you would have a Java object whose methods you could call, that's not the case. You have a JSON representation of the object which you can extract data from using Javascript code.
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
Thanks for your reply paul. What i understood is call the java class and so the method. Get the object and get the data through its properties and then convert it into json and send it back to ajax code.
Json code will be implemented in java class . Am i right?
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
|
Thanks Bear
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
Kunal Lakhani wrote:
Json code will be implemented in java class . Am i right?
I would not write JSON conversion as part of any class -- there are many JSON libraries you can use. Gson, Jackson, Stringtree, etc...
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
|
Cannot google due to some limitations. So,after getting the data through properties,what these libraries will do? If some one can provide me a link of dealing with objects,json and ajax in java
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
|
Gson
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
Thanks Bear
Here is what i tried (before json gets into action)
This url, inside ajax code, calls my servlet
Servlet
I am using Hibernate
DAO class
This Code then runs
But, when i change my dao class to include Hibernate related objects , like this
DAO Class
The code "System.out.println(policynumber); " doesn't prints anything and the failure code in ajax runs with "alert(request.statusText);" as "Internal server error"
Still, wonder why?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
|
If the servlet or other backend code is failing, that needs to be diagnosed. It has nothing to do with the client. A 500 error usually means that an unexpected error has been thrown. Starts with the server logs.
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
Problem Solved. Actually, i committed a very silly mistake.
I am using a Hibernate utility class to get the session. But in my cfg.xml i forgot to configure thread for current session , i.e "<property name="current_session_context_class">thread</property>"
Thanks Bear. Now, looking forward to JSON
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
Solved
Thanks to all
|
 |
 |
|
|
subject: Getting data from database using ajax
|
|
|