我正在尝试学习 AJAX,现在我正在阅读 servlet 和回调。我觉得我的书没有详细介绍 servlet 或回调,所以我查看了许多在线资源,结果更加困惑。我觉得我会通过实例来理解它,所以我指望你们帮助我:) 将不胜感激!我要做的是创建一个接受数字的索引页面,将其异步传递给 servlet 以求平方,然后在索引页面上显示结果。这是我到目前为止得到的。
索引.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
function setup() {
if (window.XMLHttpRequest) {
r = new XMLHttpRequest();
} else {
r = new ActiveXObject("Microsoft.XMLHTTP");
}
r.open("get","convert",true);
r.onreadystatechange=???????????
r.send(null);
}
</script>
</head>
<body onload="setup()">
<h3>Enter a number to be squared <input type="text" name="number" size="2"
maxlength="4"/></h3>
<p id="result"></p>
</body>
转换.java
package squared;
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;
import java.sql.*;
public class convert extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out=response.getWriter();
}
}
感谢您花时间帮助我!