title: Javascript calling a java Servlet and return data help
body: <p>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! :)</p>
  
  <p><strong>Overview:</strong> I need to get Javascript to call my servlet and return data to update the value on a form.</p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>I found this code on the net.  and modified it to work with what I have.</p>
  
  <pre><code>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 
  </code></pre>
  
  <p>}</p>
  
  <p>function addrequest(req) {</p>
  
  <pre><code>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;}
  </code></pre>