regist.jsp 文件:一个简单的注册页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <%@ page contentType="text/html; charset=gb2312"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>注册页面</title> <script type="text/javascript" src="http://www.knowsky.com/js/ajax.js"> </script> <script type=text/javascript> <!-- function myAlert(strTitle) { var message=document.getElementById("myDiv").innerHTML; var win1 = new Zapatec.AlertWindow(message, {title:strTitle, modal:true, width : 580,height:330}); } function doCheck() { var f = document.forms[0]; if(f.username.value!="") { document.getElementById("feedback_info").innerHTML = "系统正在处理您的请求,请稍后。"; send_request("GET","checkUsername.jsp?username="+f.username.value,null,"text",showFeedbackInfo); } else { document.getElementById("feedback_info").innerHTML = "请输入用户名称。"; } } function showFeedbackInfo() { if (http_request.readyState == 4) { if (http_request.status == 200) { document.getElementById("feedback_info").innerHTML = http_request.responseText; } else { alert("您所请求的页面有异常。"); } } }
</script> </head> <body> <form name="form1" method="post" action=""> <table style="font-size:12px;"> <tr> <td width="80">用户名:</td> <td><input type="text" name="username" onblur="doCheck()"></td> </tr> <tr> <td colspan="2"><span id="feedback_info" style="color:#FF0000"></span></td> </tr> <tr> <td>一级密码:</td> <td><input type="password" name="pwd"></td> </tr> </table> </form> </body> </html>
|
js文件源代码如下:(ajax.js)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| var http_request = false;
function send_request(method,url,content,responseType,callback) { http_request = false; if(window.XMLHttpRequest) { http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { http_request.overrideMimeType("text/xml"); } } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { window.alert("不能创建XMLHttpRequest对象实例."); return false; } if(responseType.toLowerCase()=="text") { http_request.onreadystatechange = callback; } else if(responseType.toLowerCase()=="xml") { http_request.onreadystatechange = callback; } else { window.alert("响应类别参数错误。"); return false; } if(method.toLowerCase()=="get") { http_request.open(method, url, true); } else if(method.toLowerCase()=="post") { http_request.open(method, url, true); http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); } else { window.alert("http请求类别参数错误。"); return false; } http_request.send(content); }
function processTextResponse() { if (http_request.readyState == 4) { if (http_request.status == 200) { alert("Text文档响应。"); } else { alert("您所请求的页面有异常。"); } } }
function processXMLResponse() { if (http_request.readyState == 4) { if (http_request.status == 200) { alert("XML文档响应。"); } else { alert("您所请求的页面有异常。"); } } }
|
checkUsername.jsp源代码如下:
1 2 3 4 5 6 7 8 9 10 11
| <%@ page contentType="text/html; charset=gb2312"%> <%@ page import="com.kemei.user.util.MemberManager" %> <% String name=request.getParameter("username"); MemberManager manager=new MemberManager(); if(manager.searchByUsername(username)) out.println("用户名称["+username+"]已经被注册,请更换其他用户名称再注册。"); else out.println("用户名称["+username+"]尚未被注册,您可以继续。"); manager.closeDAO(); %>
|
到此,一个简单的异步验证用户名的程序已经完成,当你输入完用户名后,切换光标,将会异步验证数据的正确性,但是,在使用时还遇到点不问题,最初输入英文或数字验证用户名时,没问题,但我输入中文难时却出现乱码,于是对checkUsername.jsp进行了修改,修改后源程序如下:
1 2 3 4 5 6 7 8 9 10 11 12
| <%@ page contentType="text/html; charset=gb2312"%> <%@ page import="com.kemei.user.util.MemberManager" %> <% String name=request.getParameter("username"); String username=new String(name.getBytes("ISO8859-1"),"gb2312"); MemberManager manager=new MemberManager(); if(manager.searchByUsername(username)) out.println("用户名称["+username+"]已经被注册,请更换其他用户名称再注册。"); else out.println("用户名称["+username+"]尚未被注册,您可以继续。"); manager.closeDAO(); %>
|