使用Ajax调用servlet

时间:2014-10-16 05:52:24

标签: javascript ajax jsp servlets

提前谢谢你。 我使用ajax调用servlet。我无法从脚本中获取servlet的响应。我的jsp文件看起来像这样

<%@ 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>

我的服务器文件(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");

    }

    }

请帮帮我。我无法调用servlet。

1 个答案:

答案 0 :(得分:0)

为什么不使用jquery进行ajax调用。在下面的示例中,当id为$.get()的按钮时,将使用jquery函数#ajaxbtn从ajax调用返回来自servlet(f1.java)的数据,即字符串“Welcome”。点击。 data中包含的返回数据会附加到ID为#destajax的div中。

<%@ 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文件(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");

    }

    }