日期:2014-05-16  浏览次数:20697 次

Ajax自动验证用户是否存在
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	
	<script type="text/javascript">
		var XmlHttpRequest;
		function createXMLHttpRequest(){
			if(window.ActiveXObject){
				XmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
			}else{
				XmlHttpRequest=new XMLHttpRequest();
			}
		}	

		function checkName(uname){
			createXMLHttpRequest();
			XmlHttpRequest.open("post","CheckUserServlet","true");
			XmlHttpRequest.onreadystatechange=callback;
			XmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			XmlHttpRequest.send("uname="+uname.value);
		}		
		
		function callback(){
			if(XmlHttpRequest.readyState==4&&XmlHttpRequest.status==200){
				var bl=XmlHttpRequest.responseText;
				if(bl=="true"){
					document.getElementById("id1").innerHTML="<font color=red>用户名不能使用</font>";
				}else{
					document.getElementById("id1").innerHTML="";
				}
			}
		}
    </script>
  </head>
  
  <body>
  <form action="CheckUserServlet" method="post">
  	用户名:<input type="text" name="uname" onblur="checkName(this)"><div id="id1" style="display:inline"></div><br/>
  	密码:<input type="password" name="pwd"> 
  	<input type="submit">
  </form>
  </body>
</html>

?