• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null values being returned from servlet

 
Johnny Chan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do


what does that return?

Eric
 
Johnny Chan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to close and flush the ServletOutputStream something like this:

 
Johnny Chan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, when i go to: http://localhost:8080/TestServlet I see:
{"optionValue":"1","optionDisplay":"2009"}



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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I had only enabled "script" for debugging in firebug. I see what you were referring to.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wouldn't changing

$.getJSON("/TestServlet",

to

$.getJSON("TestServlet",

or

$.getJSON("/app/TestServlet",

solve your problem?

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The latter format, which starts with the application context path, is the only correct way to address servlets.
 
Johnny Chan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic