回顾Ajax的实现过程,包含前端代码和后台Java代码。
通过给一个button添加click函数去调用sunServlet,来发起一个Ajax请求。注意open方法的第二个参数是MappingURL。
(3)测试
控制台输出。

(1)后台创建过程
1.使用MyEclipse10创建Web工程。
2.新建servlet,注意不是新建class
使用MyEclipse会自动为servlet配置web.xml文件。
(2)前台代码
主要是JS代码,通过post方式去调用刚刚创建的servlet。
function funServlet(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
console.log("readyState:"+xmlhttp.readyState+";status"+xmlhttp.status);
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
alert(xmlhttp.responseText);
}
};
xmlhttp.open("post","./servlet/servletForAjax",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send("name=allen&password=123456");
}
通过给一个button添加click函数去调用sunServlet,来发起一个Ajax请求。注意open方法的第二个参数是MappingURL。
(3)测试
控制台输出。
</pre><pre>