简体   繁体   English

使用Ajax调用servlet

[英]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. 我正在使用ajax调用servlet。我无法从脚本中的servlet获得响应。 my jsp file looks like this 我的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>

My servelet file(f1.java) looks like this 我的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");

    }

    }

Please help me. 请帮我。 i am not able to invoke servlet. 我无法调用servlet。

Why not use jquery for making ajax calls. 为什么不使用jquery进行ajax调用。 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. 在下面的示例中,当单击ID为#ajaxbtn的按钮时,将使用jquery函数$.get()从ajax调用返回servlet(f1.java)中的数据,即字符串“ Welcome”。 This returned data contained in data is appended to the div with id #destajax . 这返回包含在数据data被追加到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) 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");

    }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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