title: Receiving a single line of text from servlet using XMLHttp request
body: <p>I am trying to send an XMLHttp request from a JSP to a servlet and display a message in alert() received from server. But I am basically receiving the <code>&lt;script&gt; &lt;script/&gt;</code> part with some html tag.</p>
  
  <p>Here is my jsp code:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%&gt;
    &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org    /TR/html4/loose.dtd"&gt;
   &lt;html&gt;
   &lt;head&gt;
   &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
   &lt;title&gt;Insert title here&lt;/title&gt;
  
   &lt;script&gt;
      var xmlHttp;
  
      function createXMLHttpRequest() {
        if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest) {
          xmlHttp = new XMLHttpRequest();
        }
      }
     function startRequest() {
     createXMLHttpRequest();
     xmlHttp.onreadystatechange = handleStateChange;
     xmlHttp.open("post", "testServlet", true);
     // var msg="Hello world";
     xmlHttp.send(null);
    }
     function handleStateChange(){
      if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
          alert("The server replied with: " +xmlHttp.responseText);
        }
      }
     }
   &lt;/script&gt;
   &lt;/head&gt;
   &lt;body&gt;
  &lt;form action="#"&gt;
  &lt;input type="button" value="Press ME!!"
   onclick="startRequest();"/&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is my servlet code:</p>
  
  <pre><code> package pk1;
  
   import java.io.IOException;
   import java.io.PrintWriter;
  
   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("/testServlet")
    public class testServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
      public testServlet() {
       super();
      }
  
  
  protected void doGet( HttpServletRequest request,                            
  
       HttpServletResponse response)throws ServletException, IOException {
  
      test(response);
   }
  
  
  protected void doPost(HttpServletRequest request,   
  
      HttpServletResponse response) throws ServletException, IOException {
  
      test(response);
  }
  
  public void test(HttpServletResponse response) throws IOException{
      response.setContentType("text/plain");
      response.setHeader("Cache-Control", "no-cache");
      response.setHeader("Pragma", "no-cache");
      response.setCharacterEncoding("UTF-8");
  
       String data = "This is a response from testServlet";
      PrintWriter out=response.getWriter();
      out.write(data);
  
      out.flush();
      out.close();
  
      }
  
  }
  </code></pre>
  
  <p>Since I am using Tomcat7 I guess I don't need to specify URL mapping? so what am I doing wrong here? kindly let me know.</p>