JAVA AJAX教程第一章—初识AJAX
2009-10-27 00:00:00 来源:WEB开发网采用AJAX后的MVC设计模式,如图:
图片看不清楚?请点击这里查看原图(大图)。
文件层次如下:
先来看View,视图部分的代码,login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<script language="javascript">
var XMLHttpReq = false;
function createXMLHttpRequest(){
if(window.XMLHttpRequest){
XMLHttpReq = new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
XMLHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
}catch(e){
try{
XMLHttpReq = new ActiveXObject("Mircsoft.XMLHTTP");
}catch(e1){}
}
}
}
function sendRequest(url){
createXMLHttpRequest();
XMLHttpReq.open("GET",url,true);
XMLHttpReq.onreadystatechange = processResponse;
XMLHttpReq.send(null);
}
function processResponse(){
if(XMLHttpReq.readyState == 4){
if(XMLHttpReq.status == 200){
var res = XMLHttpReq.responseXML.getElementsByTagName("res")[0].firstChild.data;
window.alert(res);
document.myform.uname.value="";
document.myform.pwd.value="";
}else{
window.alert("你请求的页面有异常");
}
}
}
function userCheck(){
var uname = document.myform.uname.value;
var pwd = document.myform.pwd.value;
if(uname == ""){
window.alert("用户名不能为空");
document.myform.pwd.value="";
document.myform.uname.focus();
return false;
}else{
sendRequest("login?uname="+uname+"&pwd="+pwd);
}
}
</script>
<form action="" method="post" name="myform">
用户名:<input type="text" name="uname">
密码:<input type="password" size="20" name="pwd">
<input type="button" value="login"
</form>
</body>
</html>
更多精彩
赞助商链接