2009-03-30
<script type="text/javascript">
function validate()
{
var idField = document.getElementById("userid");
var url = "validate.jsp?id="+escape(idField.value);
if(window.XMLHttpRequest)
{
req = new XMLHttpRequest();
}else if(window.ActiveXObject)
{
req = new ActiveXObject("Microsoft.XMLHttp");
}
req.open("Get",url,true);
req.onreadystatechange = callback;
req.send(null);
}
function callback()
{
if(req.readyState == 4)
{
if(req.status == 200)
{
var msg = req.responseXML.getElementsByTagName("msg")[0];
setMsg(msg.childNodes[0].nodeValue);
}
}
}
function setMsg(msg)
{
if(msg == "valid")
document.getElementById("usermsg").innerHTML = "<font color='green'>可以使用</font>";
else
document.getElementById("usermsg").innerHTML = "<font color='red'>用户名无效</font>";
}
</script>
</head>
<body>
<form action="loginOk.jsp" method="post">
用 户 名:
<input type="text" name="username" id="userid" οnblur="validate()"><div id="usermsg"> </div>
<br>
密 码:
<input type="text" name="password">
<br>
</form>
</body>
------------------------------------------------------------------------------------
validate.jsp
------------------------------------------------------------------------------------
<%
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expries", 0);
String username = request.getParameter("id");
if(username.equals("admin"))
response.getWriter().write("<msg>invalid</msg>");
else
response.getWriter().write("<msg>valid</msg>");
%>