title: Calling servlet using Ajax
body: <p>Thank you in advance.
  I am calling the servlet using ajax.I am not able to get response from servlet in script. my jsp file looks like this</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%&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;
   function makeRequest()
   {
        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safarixmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {  
       if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
        {
  
            var val =document.getElementById("t1").value;
            alert(xmlhttp.status);
            alert(xmlHttpRequest.responseText);
            document.getElementById("mydiv").value=xmlHttpRequest.responseText;
        }
    };
    /*xmlhttp.open('GET','http://localhost:7001/ajaxx/f1',true); */
     xmlhttp.open('GET','f1.java',true); 
    xmlhttp.send();
    }
  
    &lt;/script&gt;
  
    &lt;/head&gt;
    &lt;body&gt;
    &lt;form name="f"&gt;
    &lt;p&gt; Enter the name &lt;/p&gt;
    Name:&lt;input type="text" id="t1"&gt; &lt;br/&gt;
    &lt;input type="button" name="b1" value=" CLICK TO CONECT TO SERVER" onclick=makeRequest()&gt;
    &lt;br/&gt; 
    &lt;div id="myDiv"&gt;&lt;h2&gt; AJAX &lt;/h2&gt;&lt;/div&gt;
    &lt;/form&gt;
    &lt;/body&gt;
    &lt;/html&gt;
  </code></pre>
  
  <p>My servelet file(f1.java) looks like this</p>
  
  <pre><code>  package ajaxx;
  
    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 f1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public f1()
    {
       super();
      // TODO Auto-generated constructor stub
    }
  
  
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws                            
     {
      System.out.println("hello"); 
      // TODO Auto-generated method stub
      response.setContentType("text/html");
      PrintWriter pw=response.getWriter();
      response.setCharacterEncoding("UTF-8");
      pw.write("Welcome");
  
      }
  
      }
  </code></pre>
  
  <p>Please help me. i am not able to invoke servlet.</p>