views:

1255

answers:

1

I don't have much experience with servlets and I've been going around in circles search for an answer to my issue. So it' time to get the experts answers! :)

Overview: I need to get Javascript to call my servlet and return data to update the value on a form.

I have a java servlet running in Glassfish V2.1 called DBGet, the purpose of which is to return a string of data from an mysql database. I no problems coding that part.

When I try to get javascript to access the DBGet servlet I'm getting an XML Response object back that I really don't know how to parse to get the data. I'm not even sure if this is the best method to use. So I'm open to other solutions.

I found this code on the net. and modified it to work with what I have.

function ajaxLoad(logid) {
var servlet = "DBGet";          //the name (URI) of the sevlet
var arg = "logid=" + logid;     //attributes
var req = servlet + "?" + arg;  //compiling the request

addrequest(req);                          //calls the addrequest function
request.onreadystatechange = function(){  //this is used to listen for changes in the request's status
 if(this.readyState == 2) {
   //not sure what to do here.....
  }
 }
alert(request.toString()); //for testing

}

function addrequest(req) {

try {                                     //create a request for netscape, mozilla, opera, etc.
    request = new XMLHttpRequest();
}catch (e) {

    try {                                 //create a request for internet explorer
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }catch (e) {                           //do some error-handling
        alert("XMLHttpRequest error: " + e);
    } }

request.open("GET", req, true);       //prepare the request
request.send(null);     //send it

return request;}
A: 

The javascript technique you're using to retrieve data from the server is often called Asynchronous Java And XML (AJAX). You will want to set up the onreadystatechange method in addrequest(), before calling send(); what you want to do is pass in a callback method to addrequest(), and:

if (http.readyState == 4 && http.status == 200) {
  callback(http);
}

A request goes out when send() is called; the onreadystatechange() method is fired when the response is received from the server. This can be done synchronously (the browser will hang until the response is received) or asynchronously (the sending method exits and other code/behavior can run until the onreadystatechange() is triggered). In your callback method:

-http.responseText has the result of the request

-http.responseXML has a DOM-style XML document object, if the response comes in valid XML format.

-Check the http.responseXML.parseError.errorCode (0 indicates success)

-If there is an error, http.responseXML.parseError.reason shows the error message

-Note that the AJAX call may be a "success" but the server may send back an error response; you should check for this, also.

RMorrisey
An intro to AJAX:http://www.w3schools.com/Ajax/ajax_intro.asp
RMorrisey
I made the changes, but I'm still not getting anything back from the javascript call. ?? what can I do to narrow this down to the issue?
Rick
Thank you for your advice - it helped and I got it working. I'm a happy programmer for the next 90 milliseconds!
Rick
Glad you got it fixed =) Sorry I didn't see your last comment earlier
RMorrisey