title: Call servlet from Ajax
body: <p>This is my jsp.. and I use Javascript-function and ajax-call to call servlet (ReadprojectInfo).</p>
  <pre><code>&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
      pageEncoding=&quot;ISO-8859-1&quot;%&gt;
  
  &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script type=&quot;text/javascript&quot;&gt;
  function displayProject()
  {
      var xmlhttp;
      if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        }
      else
        {// code for IE6, IE5
        xmlhttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
        }
      
      xmlhttp.open(&quot;GET&quot;,&quot;ReadProjectsInfo&quot;,true);
      xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xmlhttp.onreadystatechange= function ()
      {
          if (xmlhttp.readyState==4)
          {
          if (xmlhttp.status == 200)
          {
          var time = xmlhttp.responseText;
          //alert(time);
          document.getElementById(&quot;center&quot;).innerHTML=xmlhttp.responseText;
          }
          }
      }
      xmlhttp.send(); 
  
  //document.getElementById(&quot;center&quot;).innerHTML=Date();
  }
  
  &lt;/script&gt;
  
  
  &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/start.css&quot; /&gt;
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;&gt;
  &lt;title&gt;Learning CSS&lt;/title&gt;
  &lt;/head&gt;
      &lt;body&gt;
      &lt;div id =&quot;headerLink&quot; class=&quot;HeaderLink&quot; &gt;
          &lt;button id=&quot;adminLink&quot; class=&quot;AdminLink&quot; href='DNE.jsp'&gt;Home&lt;/button&gt;&lt;/div&gt;
          &lt;button id=&quot;projectButton&quot; class=&quot;ProjectButton&quot;  onclick=&quot;displayProject()&quot; &gt;Projects&lt;/button&gt;
      &lt;/div&gt;
      
          &lt;div id=&quot;center&quot; class =&quot;Center&quot;&gt;&lt;p&gt;Click Project&lt;/p&gt;&lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Servlet code is as follows:</p>
  <pre><code>package excelExchange;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javaFiles.Dog;
  import javaFiles.Person;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  public class ReadProjectsInfo {
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
      {
      doGet(request, response);
      }
      
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
          //String var1= request.getParameter(&quot;var&quot;);
          
          response.setContentType(&quot;text/html&quot;);
          PrintWriter out = response.getWriter();
          out.write(&quot;Hello from servlet !!&quot;);
          out.println(&quot;Hello&quot;);
          
      }
  
  }
  </code></pre>
  <p>My web.xml  look like:</p>
  <pre><code>    &lt;servlet&gt;
      &lt;servlet-name&gt;projectInfo&lt;/servlet-name&gt;
      &lt;servlet-class&gt;excelExchange.ReadProjectsInfo&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;projectInfo&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/ReadProjectsInfo&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  </code></pre>
  <p>Nothing is happening on clicking on &quot; Project &quot; button.
  Please help me out in this.</p>