title: Handling servlet output in AJAX
body: <p>My problem: I am sending a request to a servlet from an AJAX function in a JSP.</p>
  
  <p>The servlet processes the data and returns a <code>ArrayList</code>.</p>
  
  <p>My question is how to handle the <code>ArrayList</code> inside AJAX, and display it as a table in same JSP.</p>
  
  <p>The code is </p>
  
  <pre><code>function ajaxFunction ( ) {
  
   // var url= codeid.options[codeid.selectedIndex].text;
   url="mstParts?caseNo=9&amp;cdid=QCYST0020E1";
   //  alert(cid);
     var httpRequest;
      if (window.XMLHttpRequest) {
          httpRequest = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
    if (httpRequest == null){ alert('null');}
  
  alert(url);
      httpRequest.open("GET", url, true );
  
     httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
    //httpRequest.setRequestHeader('Content-Type', 'text/plain');
      httpRequest.send(null);
  
    alert('t1');
  }
  
  function alertContents(httpRequest) {
      if (httpRequest.readyState == 4) {
          var cType =httpRequest.getResponseHeader("Content-Type");
          //document.write(httpRequest.toString());
        // alert(cType);
         // var xmlDoc=httpRequest.responseText;
          //document.write(xmlDoc.toString());
        //  if (xmlDoc == null) {alert('null returned');}
          if (!httpRequest.status == 200) {
              alert('Request error. Http code: ' + httpRequest.status);
          }
          else
              {
                  var profileXML = eval(&lt;%=request.getAttribute("data")%&gt;);
                  if ( profileXML != null){ alert('null'); }//else { alert(profileXML(0)); }
                 // httpRequest.getAttribute("data");
  
  
              }
      }
  }
  </code></pre>