title: Servlet response to AJAX request is empty
body: <p>I am sending an AJAX request using javascript to a servlet. The servlet is indeed replying but the response header is null and so is the response text.</p>
  
  <p>When I try the same client code to instead send the request to a php page it works fine.</p>
  
  <p>Here are the two clients (you can try them and look at their sources):</p>
  
  <ul>
  <li>ajax-to-servlet: <strike>http://79.136.61.27/web/ajax-to-servlet.html</strike></li>
  <li>ajax-to-php: <strike>http://79.136.61.27/web/ajax-to-php.html</strike></li>
  </ul>
  
  <p>The output when sending the request to the servlet is:</p>
  
  <pre><code>Response will go below
  
  Response:
  
  responseText was null!
  
  Headers:
  
  null response headers!
  </code></pre>
  
  <p>The output when sending the request to PHP is:</p>
  
  <pre><code>Response will go below
  
  Response:
  
  Hi from php
  
  Headers:
  
  Date: Sun, 17 Apr 2011 11:58:57 GMT Server: Apache/2.2.17 (Win32) PHP/5.3.6 X-Powered-By: PHP/5.3.6 Content-Length: 18 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html
  </code></pre>
  
  <p>Here is the code for the servlet. As you can see I am experimenting a bit with settings headers and content type but none of my experiments seem to have any effect. The weird thing is, that I did a hello world-example like this using servlets just recently and it worked just fine without messing with headers and stuff. But now it just does not work anymore. :(</p>
  
  <pre><code>package simple;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  
  public class SimpleServlet extends HttpServlet {
  
      private static final long serialVersionUID = -6713061702557291351L;
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          String out = "&lt;p&gt;Hi from servlet!&lt;/p&gt;";
  
          response.setContentType("text/xml");
          response.setHeader("Cache-Control", "no-cache");
          System.out.println("got request");
  
          PrintWriter pw = response.getWriter();
  
          pw.write(out);
          pw.flush();
          boolean error = pw.checkError();
          System.out.println("Error? " + error);  
      }
  }
  </code></pre>
  
  <p>hifromphp.php is simply:</p>
  
  <pre><code>&lt;?php
      echo "&lt;p&gt;Hi from php&lt;/p&gt;";
  ?&gt;
  </code></pre>
  
  <p>Thanks for reading and thanks in advance!</p>
  
  <p>Edit: I realized that those links will not work forever. So, for archive purposes I am pasting ajax-to-servlet.html here. ajax-to-php.html is identical except the URL where the request goes.
  ajax-to-html.html:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head&gt;
      &lt;title&gt;Send ajax&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;script type="text/javascript"&gt;
              /* &lt;![CDATA[ */
              function getXMLHttp() {
                  var xmlHttp = new XMLHttpRequest();
  
                  return xmlHttp;
              }
  
              function sendAjax() {
                  var xmlHttp = getXMLHttp();
                  var divResp = document.getElementById("response");
                  var divHdrs = document.getElementById("responseHeaders");
  
                  xmlHttp.onreadystatechange = function() {
                      if (xmlHttp.readyState == 4) {
                          var hdrs = xmlHttp.getAllResponseHeaders();
                          var resp = xmlHttp.responseText;
  
                          divHdrs.innerHTML = "&lt;p&gt;Headers:&lt;/p&gt;&lt;p&gt;" + (hdrs ? hdrs : "null response headers!&lt;p&gt;");
                          divResp.innerHTML = "&lt;p&gt;Response:&lt;/p&gt;" + (resp ? resp : "&lt;p&gt;responseText was null!&lt;p&gt;");
                      }
                  }       
  
                  xmlHttp.open("GET", "http://79.136.61.27:8080/SimpleServlet/SimpleServlet", true);
                  xmlHttp.send(null);
              }
              /* ]]&gt; */
          &lt;/script&gt;
      &lt;p&gt;&lt;input type="button" value="Send Ajax" onclick="javascript: sendAjax();"/&gt;&lt;/p&gt;
      &lt;p&gt;Response will go below&lt;/p&gt;
      &lt;div id="response"&gt;&lt;/div&gt;
      &lt;div id="responseHeaders"&gt;&lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>