后臺驗證是WEB應用中使用非常廣泛的一種應用形式,常用于驗證用戶登錄\后臺數據校驗等等.
login.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
<!--
var xmlHttp ;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") ;
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest() ;
}
}
function checkUser(){
createXMLHttpRequest() ;
var name = document.getElementById("username").value ;
var password = document.getElementById("password").value ;
var url = "login_check.jsp?username="+name+"&password="+password ;
xmlHttp.open("GET",url,true) ;
xmlHttp.onreadystatechange = showResult ;
xmlHttp.send(null) ;
}
function showResult(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var result = xmlHttp.responseText;//在頁面上顯示返回的結果
document.getElementById("checkResult").innerHTML="<b>"+result+"</b>" ;
}
}
}
//-->
</script>
</head>
<body>
ajax<br>
用戶名:<input type="text" id="username"><div id="checkResult"></div><p>
密碼:<input type="text" id="password" ><p>
<input type="button" value="檢查用戶名" onclick="checkUser()"><p>
</body>
</html>
login_check.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ajax</title>
</head>
<body>
<%
String name = request.getParameter("username") ;
String password = request.getParameter("password") ;
System.out.println("name--->"+name+" password---->"+password);
if(name != null && password != null){
if(name.equals("ajax") && password.equals("ajax")){
out.write("<font color=red>恭喜你!此用戶名可用</font>");
}else{
out.write("<font color=red>此用戶名已被人占用</font>") ;
}
}
%>
</body>
</html>