[英]Calling servlet using Ajax
先感謝您。 我正在使用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>
我的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");
}
}
請幫我。 我無法調用servlet。
為什么不使用jquery進行ajax調用。 在下面的示例中,當單擊ID為#ajaxbtn
的按鈕時,將使用jquery函數$.get()
從ajax調用返回servlet(f1.java)中的數據,即字符串“ Welcome”。 這返回包含在數據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文件(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.