我想对 servlet 进行 ajax 调用并打印响应。我已经编写了index.html
, Assignemnt
servlet 和web.xml
如下,但我没有收到警报。出了什么问题?
index.html
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
<script>
$(document).ready(function() {
$("button").click(function() {
$.get("Assignment", function(data, status) {
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
<title>Insert title here</title>
</head>
<body>
<form method="GET" action="Assignment" name="showall">
<table>
<tr>
<td><input type="checkbox" name="id1" /></td>
<td>Jim</td>
<td>Knopf</td>
</tr>
<tr>
<td><input type="checkbox" name="id2" /></td>
<td>Jim</td>
<td>Bean</td>
</tr>
</table>
<p>
<button>Send an HTTP GET request to a page and get the
result back</button>
</p>
</form>
</body>
</html>
小服务程序:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.err.println("-------++++");
String data = "Hello World!";
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(data);
//RequestDispatcher rd = request.getRequestDispatcher("/Assignment");
RequestDispatcher rd = request.getRequestDispatcher("/index.html");
rd.forward(request, response);
}
Servlet 映射:
<servlet>
<description>FirstAssignment</description>
<display-name>Assignment</display-name>
<servlet-name>Assignment</servlet-name>
<servlet-class>com.Assignment</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Assignment</servlet-name>
<url-pattern>/Assignment</url-pattern>
</servlet-mapping>