| Author |
Null values being returned from servlet
|
Johnny Chan
Greenhorn
Joined: Mar 05, 2009
Posts: 14
|
|
I'm trying to get data back from a servlet in JSON format. I am using jquery, and have written a simple test just to return some data and then alert me of it in an html page. However, everytime i reload the page, the value of the data passed back from the servlet is null. If i access the servlet directly, i can see the data posted to the screen.
Here is my webpage's code:
Firebugs tells me the parameter "json" is null.
And here is my servlet code:
As I said, when i go to: http://localhost:8080/TestServlet I see:
{"optionValue":"1","optionDisplay":"2009"}
in text - which is i believe what i want. I've used cactus on the servlet as a test, and it returns the json data, but is it formatted correctly? Am i somehow now sending it back in good format for jQuery? Your ideas are welcomed.
-Johnny
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
If you do
what does that return?
Eric
|
 |
Johnny Chan
Greenhorn
Joined: Mar 05, 2009
Posts: 14
|
|
Same result, json is null according to firebugs. It never even gets into the function(json). After .getJSON returns and presumably has called the servlet, and its about to call function(json), json is null and the next step is out of the execution.
I do get 1 error in the error console,
window.console is undefined
http://localhost:8080/test Line 47.. of which there is none. I'm not sure this has any bearing on the problem though.
|
 |
Omar Al Kababji
Ranch Hand
Joined: Jan 13, 2009
Posts: 357
|
|
try to close and flush the ServletOutputStream something like this:
|
Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
|
 |
Johnny Chan
Greenhorn
Joined: Mar 05, 2009
Posts: 14
|
|
|
I added that into the servlet code, and same result. I recall trying something similar, but not using flush. json variable is still null.
|
 |
Omar Al Kababji
Ranch Hand
Joined: Jan 13, 2009
Posts: 357
|
|
try to print the value of the string index.toString(); on the console to see what it contains, may be its a null value. i think this is why you are getting null.
|
 |
Omar Al Kababji
Ranch Hand
Joined: Jan 13, 2009
Posts: 357
|
|
sorry didn't read this one. are you sure that using jQuery you are really calling the servlet ?? put some logging in the doPost/doGet method of your servlet that notifies you that it had been called.
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
On the server you should be setting the content type as text/javascript.
When you look at the XMLHttpRequest in the firebug console, do you see the returned text in the response?
Eric
|
 |
Johnny Chan
Greenhorn
Joined: Mar 05, 2009
Posts: 14
|
|
I'm not sure where i find that in firebugs. I can tell you that the function "success" in the ajax() function of jquery does not appear to be run.
I am not using XML though, i am using JSON.
Edit:
I see that jquery getJSON function should return XMLHttpRequest, but i don't see that in firebugs. I will look.
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
On console there is an options link on the right side. Click on that and select "Show XmlHttpRequests"
They will show up in the console window when they are run.
Eric
|
 |
Johnny Chan
Greenhorn
Joined: Mar 05, 2009
Posts: 14
|
|
|
Okay, I had only enabled "script" for debugging in firebug. I see what you were referring to.
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
wouldn't changing
$.getJSON("/TestServlet",
to
$.getJSON("TestServlet",
or
$.getJSON("/app/TestServlet",
solve your problem?
Eric
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56229
|
|
|
The latter format, which starts with the application context path, is the only correct way to address servlets.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Johnny Chan
Greenhorn
Joined: Mar 05, 2009
Posts: 14
|
|
Okay, yup, i was getting a 404 error because i referring to /TestServlet, instead of /app/TestServlet. That's why i'm a greenhorn! Thanks for your help. One other question. If i'm accessing a potentially large dataset in the servlet that is in a mysql database, is JSON the preferred method or is it XML? I just wanted to gauge your thoughts on the matter.
-JC
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56229
|
|
I rarely use XML.
On the other hand, for something as complex as that, I'd format the result in a JSP and send the resulting HTML fragment back as the result to plunk into the DOM rather than having to parse data and build the DOM in script.
|
 |
 |
|
|
subject: Null values being returned from servlet
|
|
|