0

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

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Insert title here</title>
 <script >
 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 && 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();
  }

  </script>

  </head>
  <body>
  <form name="f">
  <p> Enter the name </p>
  Name:<input type="text" id="t1"> <br/>
  <input type="button" name="b1" value=" CLICK TO CONECT TO SERVER" onclick=makeRequest()>
  <br/> 
  <div id="myDiv"><h2> AJAX </h2></div>
  </form>
  </body>
  </html>

My servelet file(f1.java) looks like this

  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");

    }

    }

Please help me. i am not able to invoke servlet.

VPK
  • 3,010
  • 1
  • 28
  • 35
Niketa
  • 453
  • 2
  • 9
  • 24
  • possible duplicate of [How to use Servlets and Ajax?](http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax) – Mahesh Oct 16 '14 at 06:05
  • 1
    you cant call a servlet with the .java extension.. in xmlhttp.open('GET','f1.java',true); must be xmlhttp.open('GET','f1',true); – Nomesh DeSilva Oct 16 '14 at 13:01
  • Did you even compile the servlet and check it normally first? Make sure stuff works normal before messing with Ajax. And it would be better to start using jQuery to do the Ajax. The old manual way requires too many things to get it to work right cross-platform that you will undoubtedly miss. – developerwjk Oct 16 '14 at 23:57
  • 1
    By the way, your problem is partially that you have typed the name of your XMLHttpRequest variable inconsistently. – developerwjk Oct 17 '14 at 00:01

1 Answers1

0

Why not use jquery for making ajax calls. In the below example, data from the servlet(f1.java) i.e the string "Welcome", will be returned from the ajax call, by using the jquery function $.get(), when the button with id #ajaxbtn is clicked. This returned data contained in data is appended to the div with id #destajax.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Insert title here</title>
    <!--include JQuery library-->
 <script src="js/jquery-1.11.3.js" charset="utf-8"></script>
    <!--Using JQuery to make ajax call-->
 <script type="text/javascript">
  $(document).ready(function(){
   $('#ajaxbtn').on('click', function(){
     $.get("f1", function(data){
        // the response from servlet(f1) is contained in data
        // using ajax to append data from servlet(f1) to div
       $('#destajax').html(data);
     })
   })
  })
 </script>


 </head>
 <body>
 <form name="f">
 <p> Enter the name </p>
  Name:<input type="text" id="t1"> <br/>
 <input type="button" name="b1" id="ajaxbtn" value=" CLICK TO CONECT TO SERVER">
 <br/> 
 <div id="myDiv"><h2> AJAX </h2></div>
  <!--data("Welcome") from servlet(f1) is appended to the below div-->
 <div id="destajax"><div>
 </form>
 </body>
 </html>

Servelet file(f1.java)

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");

    }

    }
lordvcs
  • 2,466
  • 3
  • 28
  • 37