• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AJAX script isn't parsing XML response from servlet

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I am somewhat new to Java servlet programming. I am using Eclipse Indigo Release IDE and it is running the Tomcat 7 server. The client is using Firefox 7.0.1. I can get the JSP file to load the Javascript function and it successfully sends a POST to the servlet. The servlet successfully queries a MySQL database using a JDBC connection and creates the XML responseString. the problem I am having is that I cannot determine if the string is successfully being returned to the JSP file or if the Javascript file is configured correctly to parse the XML string. The data I want to display on the web page does not get displayed. Any suggestions as to what I am doing wrong? Thank you for the help.

Servlet:


responseString = The responseString is actually one long string. I added the formatting below for readability




Javascript: getInventory.js



JSP file: inventory.jsp

 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One problem I can spot is in this line:


The intention is to set a callback function to be called asynchronously by browser.
But due to the parentheses, what this actually does is make a function call immediately to gotInventoryCallback()
(which is why you don't see the alert in that function), and then assign its return value (null value) to req.onreadystatechange, which is meaningless.

The correct code should be



This is the one I could spot, but I didn't go through your XML implementation in detail. Try out this correction and if there are further problems, ask away on this thread.
Also, IMO, you should stop using the raw XMLHttpRequest API and start with jquery (it's not the only one out there, but it's good to start with). It helps write much more compact, readable, cross-browser error free code.
In jquery, this would have been
 
Tony Beseau
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karthik,

Thank you for the reply. Removing the parentheses allowed the alerts to work in the gotInventoryCallback function. Parsing the XML data and populating the fields still doesn't work. I have barely seen JQuery so I am not sure how to get it setup. Do you have a link to a good example that could show me how to set that up? Also, to use JQuery do I load the jquery.min.js file in the JSP file or in the javascript file?

Thanks again,
Tony
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be very helpful if you could get rid of the "JSP" from your question, since it's irrelevant. Your JSP runs on the server and generates HTML, which is sent to the browser. This HTML can of course include Javascript, as it does in your example. So we're really just discussing that HTML and the Javascript. Referring to the "JSP" just inserts more confusion into the issue.

Now, you said

the problem I am having is that I cannot determine if the string is successfully being returned to the JSP file or if the Javascript file is configured correctly to parse the XML string.



So it's time to find out whether your Javascript is receiving that XML data or not. You need a Javascript debugger. Firebug in Firefox works very well but I'm sure there are others out there.

And since this question seems to be almost entirely about Javascript, I'm going to move it to the Javascript forum.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every modern browser except for Firefox has a built-in JavaScript debugger. And as Paul pointed out, there's the Firebug plugin for Firefox.

Trying to debug JavaScript without using the debuggers is like trying to play concert piano wearing welding gloves. Get to know your tools.
 
Tony Beseau
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe playing piano with welding gloves is good music.

As I mentioned I am new to Java web development. I learned something today though and that is how to use the Javascript debugger in Firefox. As Paul and Bear pointed out it was a very useful tool and helped me solve my issue. The data is now getting populated in the web browser as I need it to. Thank you for your help and comments.

Tony

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic