title: XMLHttpRequest() is undefined Servlet
body: <p>Hello I got this script and I don't know why I always get that req = XMLHTTPRequest is undefined.
  Could you tell my what mistake I make?</p>
  
  <pre><code>var req;
  
  
  function validate() {
  //var idField = document.getElementById("name");
  var wrt1 = document.getElementById("p1").value;
  var wrt2 = document.getElementById("p2").value;
  var data = eval(wrt1)+eval(wrt2);
  //  var data = "name=" + encodeURIComponent(idField.value);
  req = new XMLHttpRequest();
  //  req = new XMLHttpRequest();
  var url = "Serwlet";
  req.open("POST", url, true);        
  req.onreadystatechange = inserter;
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  req.send(data);
  }
  
  function inserter() {
  if (req.readyState == 4) {
      if (req.status == 200) {
          var msg = req.responseText
          if (msg == "") {
              //document.getElementById("msg1").innerHTML = "&lt;div style=\"color:red\"&gt;"+"COS NIE TAK"+"&lt;/div&gt;";
              document.getElementById("msg1").value = '';
          }
          else
              //document.getElementById("msg1").innerHTML = "&lt;div style=\"color:red\"&gt;"+wynik+"&lt;/div&gt;";
              document.getElementById("msg1").value = "IT WORKS";
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><em><strong>EDIT</em></strong></p>
  
  <p>Ok I've made some improvements. Now the only problem is that msg from JavaScript inserter() is always empty ;/, don't know why, If you look at validate method the var data has correct value, the msg is always null ;/.</p>
  
  <pre><code>package Pakiet;
  
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.io.Writer;
  
  import javax.servlet.ServletException;
  import javax.servlet.ServletResponse;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.io.Writer;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  @WebServlet("/Serwlet")
  public class Serwlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  static String KOD =
  /*
  
       out.print("&lt;FORM ACTION=\"ExerciseE\" name=\"entryForm\"&gt;\n"+
                          "Name:&lt;INPUT TYPE=\"TEXT\" NAME=\"name\"&gt;&lt;BR&gt;\n"+
                          "&lt;INPUT TYPE=\"BUTTON\" VALUE=\"SUBMIT\"\n" +
                          " OnClick=\"validate()\"&gt;\n&lt;/FORM&gt;");       
   */
  "&lt;html&gt;"+
  "&lt;head&gt;"+
   "&lt;meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1250\"&gt;"+
  "&lt;title&gt;Testowanie&lt;/title&gt;"+
  "&lt;script type=\"text/javascript\" language=\"javascript\" src=\"servletD2Script.js\"&gt;&lt;/script&gt;"+
  "&lt;/head&gt;"+
  "&lt;body&gt;"+
  "&lt;div id=\"msg1\"&gt;Pierwsza suma&lt;/div&gt;"+
  "&lt;div id=\"msg2\"&gt;Pierwsza suma&lt;/div&gt;"+
  "&lt;center&gt;&lt;h2&gt;Testowanie parametrów&lt;/h2&gt;&lt;/center&gt;"+
  "&lt;hr&gt;"+
  "&lt;form method=\"get\" action=\"Serwlet\"&gt;"+
  "p1&lt;input type=\"text\" size=\"50\" name=\"p1\" onblur=\"validate()\"&gt;&lt;br&gt;"+
  "p2&lt;input type=\"text\" size=\"50\" name=\"p2\" onblur=\"validate()\"&gt;&lt;br&gt;"+
  "&lt;br&gt;&lt;input type=\"submit\" value=\"Wyślij formularz by GET\"&gt;"+
  "&lt;/form&gt;"+
  "&lt;/hr&gt;"+
  "&lt;hr&gt;"+
  "&lt;form method=\"post\" action=\"Serwlet\"&gt;"+
  "p3&lt;input type=\"text\" size=\"50\" name=\"p3\" onblur=\"funkcja()\"&gt;&lt;br&gt;"+
  "p4&lt;input type=\"text\" size=\"50\" name=\"p4\" onblur=\"funkcja()\"&gt;&lt;br&gt;"+
  "&lt;br&gt;&lt;input type=\"submit\" value=\"Wyślij formularz by POST\"&gt;"+
  "&lt;/form&gt;"+
  "&lt;/hr&gt;"+
  
  "&lt;/body&gt;&lt;/html&gt;";
  
  public Serwlet() {
      super();
  
  }
  static boolean raz = true;
  private void serviceRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
        response.setContentType("text/html");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        if(raz){out.print(Serwlet.KOD);raz = false;}
        String name = request.getParameter("name");   // dostaję nazwisko
        String org = ""; //jakas wartosc 
        out.write(org != null ?  org : "");    // zwracam organizację lub pusty napis, jesli nie znam nazwiska
  }
  
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      serviceRequest( request, response);
  }
  
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      serviceRequest( request, response);
  }
  
  }
  </code></pre>
  
  <p>AND THE SCRIPT
       //THE METHOD FUNCKJA() IS NOT IMPORTANT AT ALL</p>
  
  <pre><code>  function funkcja(){
  var wrt1 = document.getElementById("p1").value;
  var wrt2 = document.getElementById("p2").value;
  var wynik = eval(wrt1)+eval(wrt2);
  document.getElementById("msg1").innerHTML = "&lt;div style=\"color:red\"&gt;"+wynik+"&lt;/div&gt;";
  var wrt3 = document.getElementById("p3").value;
  var wrt4 = document.getElementById("p4").value;
  var wynik2 = eval(wrt3)+eval(wrt4);
  document.getElementById("msg2").innerHTML = "&lt;div style=\"color:red\"&gt;"+wynik2+"&lt;/div&gt;";
  }
  
  
  var req;
  
  function validate() {
  //var idField = document.getElementById("name");
  var wrt1 = document.getElementById("p1").value;
  var wrt2 = document.getElementById("p2").value;
  var data = eval(wrt1)+eval(wrt2)+"";
  
  if (typeof XMLHttpRequest != "undefined") {
      req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
      req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  var url = "Serwlet";
  req.open("POST", url, true);      
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  alert("data = "+data);
  req.send(data);
  req.onreadystatechange = inserter;
  }
  
  
  
  
  function inserter() {
  if (req.readyState == 4) {
      if (req.status == 200) {
          var msg = req.responseText;
          alert("msg = "+msg);
          if (msg == "") {
              document.getElementById("msg1").innerHTML = "&lt;div style=\"color:red\"&gt;"+"COS NIE TAK"+"&lt;/div&gt;";
              //document.getElementById("msg1").value = "blabla";
          }
          else
              document.getElementById("msg1").innerHTML = "&lt;div style=\"color:red\"&gt;"+msg+"&lt;/div&gt;";
              //document.getElementById("msg1").value = "COOOO JEST";
      }
  }
  }
  </code></pre>