52,792
社区成员




<html>
<head>
<title>Ajax Auto Complete</title>
<script type="text/javascript">
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function findNames() {
createXMLHttpRequest();
var url = "/servler_ajax/AutoCompleteServlet";
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
function callback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
alert(xmlHttp.responseText)
} else if (xmlHttp.status == 204){
clearNames();
}
}
}
</script>
</head>
<body>
Names: <input type="text" size="20" id="names" onkeyup="findNames();" style="height:20;"/>
<div style="position:absolute;" id="popup">
<table id="name_table" bgcolor="#FFFAFA" border="0" cellspacing="0" cellpadding="0"/>
<tbody id="name_table_body"></tbody>
</table>
</div>
OK
</body>
</html>
求人帮我改一下这个java代码,目的能传个字符串给上面的html页面中的callback回调函数,让它把“abc”alert出来,
如果上面请求的是一个jsp页面,那非常简单,直接在jsp页面中用out.write就可以了,那现在请求一个servlet如何得到同样的效果,谁帮我该一下这个java代码?
package cn.ht;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AutoCompleteServlet extends HttpServlet {
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().println("abc");
}
}