【 tulaoshi.com - Web开发 】
1.在页面的script type="text/JavaScript" 内加入
//无刷新请求的xmlhttp请求
var http_request=false;
function createRequest(url)
{
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('不能创建XMLHTTP实例!');
return false;
}
http_request.onreadystatechange=alertContents;
http_request.open("POST",url,true);
http_request.send(null);
}
2.调用--在js调用请求
createRequest('CONTROL'+'?cmd=listen');
3.在servlet中处理
PrintWriter out;
response.setContentType("html/text");//此5行必备,用于输出中文,否则乱码
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control","no-cache");
out=response.getWriter();
out.println("对不起,系统忙,请稍后再试!"); //为返回的响应结果,在页面直接以alert()显示