title: INVALID_STATE_ERR: DOM Exception 11 when I execute the req.open("Get",url,true);
body: <p>Thanks, resolved one issue, this is the next. As a novice at this, I am still getting an error and the simple application is not working. I get INVALID_STATE_ERR: DOM Exception 11 when I execute the req.open("Get",url,true); command in the javascript in located in the following index.html file. The simple appl is not working.  I get this in chrome in debug mode but the app also does not work on IE8 or FF3.  Any ideas?</p>
  
  <p>using Eclipse for J2EE with Java6, Ajax, on WindowsVista.</p>
  
  <p>index.html</p>
  
  <pre><code>    &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;script type="text/javascript"&gt;
      var req;
  
      function focusIn() {
          //This is how you comment in javascript portion of code
          //I will now demonstrate an alert function that calls a messagebox to the field, very useful for debugging
          //this displays in yoru browser
          alert('Hey dad this is an alert, this function was called by the onload message of the Body');
  
          //There is even a cooler way,  for instance say you wanted to display values
          var two = 2;
          var one = 1;
          var result = two + one;
          //Display your variable result
          alert(result);
  
          document.getElementById("key").focus();
  
      }
  
      function convertToDecimal(){
          var key = document.getElementById("key");
          var keypressed = document.getElementById("keypressed");
          keypressed.value = key.value;
          // onClick="alert('You clicked the button')"
          var url = "/AjaxResponseServlet?key=" + escape(key.value);
          if (window.XMLHttpRequest){
              req = new XMLHttpRequest();
          }
          else if (window.ActivateXObject){
              req = new ActiveXObject("Microsoft.XMLHTTP")
          }
          req.open("Get",url,true);
          req.onreadystatechange = callback;       
          req.send(null);
      }
  
      function callback() {
          if (req.readyState==4) {
              if (req.status == 200){
                  var decimal = document.getElementById("decimal");
                  decimal.value = req.responseText;
              }
          }
          clear();
      }
  
      function clear() {
          var key = document.getElementById("key");
          key.value="";
      }
  &lt;/script&gt; 
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Ajax on Java Chapter 2&lt;/title&gt;
  &lt;/head&gt;
  &lt;body onload="focusIn();" &gt;
  &lt;!-- this is how you comment in HTML Body Portion of the code --&gt;
  
  &lt;h1&gt; AJAX CHARACTER DECODER &lt;/h1&gt;
  &lt;h2&gt; Press a key to find its value. &lt;/h2&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;
              Enter Key Here --
              &lt;input type="text" id="key" name="key" onkeyup="convertToDecimal();" /&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
   &lt;/table&gt;
   &lt;br /&gt;
   &lt;table&gt;
      &lt;tr&gt;
          &lt;td colspan="5" style="border-bottom:solid black 1px;"&gt;
              Key Pressed:
              &lt;input type="text" readonly="readonly" id="keypressed" /&gt;
          &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt; Decimal &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;input type="text" readonly="readonly" id="decimal" /&gt;
              &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>
  </p>
  
  <pre><code>&lt;!-- this is how you comment in HTML Body Portion of the code --&gt;
  
  &lt;h1&gt; AJAX CHARACTER DECODER &lt;/h1&gt;
  &lt;h2&gt; Press a key to find its value. &lt;/h2&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;
              Enter Key Here --
              &lt;input type="text" id="key" name="key" onkeyup="convertToDecimal();" /&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
   &lt;/table&gt;
   &lt;br /&gt;
   &lt;table&gt;
      &lt;tr&gt;
          &lt;td colspan="5" style="border-bottom:solid black 1px;"&gt;
              Key Pressed:
              &lt;input type="text" readonly="readonly" id="keypressed" /&gt;
          &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt; Decimal &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;input type="text" readonly="readonly" id="decimal" /&gt;
              &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>My current web.xml is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  </code></pre>
  
  <p>http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    Ajax2
    
      index.html
      index.htm
      index.jsp
      default.html
      default.htm
      default.jsp
    
    
      
      AjaxResponseServlet
      AjaxResponseServlet
      com.example.servlets.AjaxResponseServlet
    
    
      AjaxResponseServlet
      /AjaxResponseServlet
    
    
      
      JAMES
      JAMES
      com.example.servlets.JAMES
    
    
      JAMES
      /JAMES
    
  
      enter code here</p>
  
  <p>My servlet code is:</p>
  
  <pre><code>package com.example.servlets;
  </code></pre>
  
  <p>/*
   * Takes a character and converts it to decimal and sends back the 
   * value in the response.
   */
  // package com.oreilly.ajax.servlet;  // causes error, so commented out</p>
  
  <p>import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;</p>
  
  <p>public class AjaxResponseServlet extends HttpServlet {</p>
  
  <pre><code>private static final long serialVersionUID = 1L;
  
  public void doGet(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {
  
      String key = req.getParameter("key");
      if (key != null) {
          // extract the first character from key
          int keyInt = key.charAt(0);
          String decimalString = Integer.toString(keyInt); 
          // setup the response
          res.setContentType("text/xml");
          res.setHeader("Cache-Control", "no-cache");
          // write out the response string
          res.getWriter().write(decimalString);
      }
      else {
          // If key comes back as a null, return a question mark.
          res.setContentType("text/xml");
          res.setHeader("Cache-Control", "no-cache");
          res.getWriter().write("?");
      }
  }
  </code></pre>
  
  <p>}</p>