简体   繁体   中英

Calling servlet using Ajax

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.

Why not use jquery for making ajax calls. In the below example, data from the servlet(f1.java) ie 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");

    }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM