ajax 注册的例子(XMLHttpRequest)

看了好久的ajax,自己动手做的例子却很少,今天决定自己动手写一个,功能很简单,实现一个简单的用户名注册功能,同时对用户输入在服务器端进行简单校验,没有用数据库保存用户名,这里利用application·对象模拟。做了好久才弄好,遇到了很多问题,最后得以解决,比较有意思的地方是当我写完例子后,在firefox下运行正常,但是在ie下有点问题,注册过的用户名还是显示注册成功,查了好久没有搞出原因,最后又复习了以前看过的一个视频教程,才知道是IE的缓存造成的,用了一个小技巧欺骗ie一下,就可以轻松搞定!感觉收获挺大的!

下面是代码:

注册页面reg.html

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3.     <head>
  4.            <title>login.html</title>
  5.         <script type="text/javascript">
  6.         
  7.             function createXmlHttpReq() {
  8.                 if(window.XMLHttpRequest) {
  9.                     xmlHttpReq = new XMLHttpRequest();
  10.                 
  11.                 } else {
  12.                 
  13.                     xmlHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
  14.                 
  15.                 }
  16.                 return xmlHttpReq;
  17.             
  18.             }
  19.             function processResponse() {
  20.                 if(xmlHttpReq.readyState == 4) {
  21.                     if(xmlHttpReq.status == 200) {
  22.                         var res = xmlHttpReq.responseText;
  23.                         document.getElementById("message").innerHTML=res;               
  24.                     
  25.                     }
  26.                 
  27.                 }
  28.             
  29.             }
  30.             function sendRequest(url) {
  31.                 url = convertUrl(url);
  32.                 createXmlHttpReq();
  33.                 xmlHttpReq.open("get",url,true);
  34.                 xmlHttpReq.onreadystatechange=processResponse;
  35.                 xmlHttpReq.send(null);
  36.                 
  37.             }
  38.             function convertUrl(url) {
  39.                 var timestamp = (new Date()).valueOf();
  40.                 if(url.indexOf("?") >= 0) {
  41.                     urlurl = url + "t=" + timestamp;
  42.                 } else {
  43.                     urlurl = url +"?t=" + timestamp;
  44.                 }
  45.                 return url;
  46.             }
  47.             function checkUsername() {
  48.                 var url = "check.jsp?type=username&username="+document.getElementById("username").value;
  49.                 sendRequest(url);
  50.             
  51.             
  52.             }
  53.             function checkPassword() {
  54.                 var url = "check.jsp?type=password&password="+document.getElementById("password").value;
  55.                 sendRequest(url);           
  56.             
  57.             }
  58.             
  59.             function checkPassword2() {
  60.                 var url = "check.jsp?type=password2&password2="+document.getElementById("password2").value+"passwordpassword="+document.getElementById("password").value;
  61.                 sendRequest(url);   
  62.             
  63.             }
  64.             
  65.             function doSubmit() {
  66.                 
  67.                 var url = "check.jsp?type=submit&username="+document.getElementById("username").value+"password="+document.getElementById("password").value+"password2password2="+document.getElementById("password2").value;
  68.                 sendRequest(url);
  69.             }
  70.         
  71.         
  72.         </script>
  73.     </head>
  74.     <body>
  75.         <h3>
  76.             REGISTER
  77.         </h3>
  78.         <img src="img/ico_loading3.gif" id="loading" style="display:none;"></img>
  79.         <div id="message" style="color:red;">
  80.         </div>
  81.         userName:
  82.         <input id="username" name="username" type="text" size="12" onblur="checkUsername()"/>
  83.         <br />
  84.         password:
  85.         <input id="password" name="password" type="password" size="10" onblur="checkPassword()" />
  86.         <br />
  87.         password2:<input id="password2" name="password" type="password" size="10" onblur="checkPassword2()" />
  88.         <br />
  89.         <button id="submit" onclick="doSubmit()">
  90.             Submit
  91.         </button>
  92.     </body>
  93. </html>

 

服务器端代码:

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String type = request.getParameter("type");
  4. if (type.equals("username")) {
  5.     String username = request.getParameter("username");
  6.     if (username != null && username.length() > 0) {
  7.         if (application.getAttribute(username) != null) {
  8.             
  9.             out.println("Username is exist!");
  10.         } else {
  11.             out.println("Username is OK!");
  12.         }
  13.         
  14.         
  15.     } else {
  16.         out.println("Username is need!");
  17.     }
  18. else if(type.equals("password")) {
  19.     String password = request.getParameter("password");
  20.     if(password != null && password.length() > 0) {
  21.         out.println("password is ok");
  22.     
  23.     } else {
  24.         out.println("Password is need!");
  25.     
  26.     }
  27. else if (type.equals("password2")) {
  28.     String password2 = request.getParameter("password2");
  29.     if(password2 != null && password2.length() > 0) {
  30.         String password = request.getParameter("password");
  31.         if( password2.equals(password)) {
  32.             out.println("Password2 is OK!");
  33.         } else {
  34.         
  35.             out.println("Password2 is not equal Password!");
  36.         }
  37.     
  38.     } else {
  39.         out.println("Password2 is need!");
  40.     
  41.     }
  42. else if (type.equals("submit")) {
  43.     boolean regFlag = true;
  44.     
  45.     String username = request.getParameter("username");
  46.     String password = request.getParameter("password");
  47.     String password2 = request.getParameter("password2");
  48.     
  49.     
  50.     if (username != null && username.length() > 0) {        
  51.         
  52.         if (username != null && username.length() > 0) {
  53.             if (application.getAttribute(username) != null) {
  54.                 regFlag = false;
  55.                 out.println("username is exist!");
  56.             } 
  57.         }
  58.                 
  59.     } else {
  60.         out.println("Username is needed!<br/>");
  61.         regFlag = false;
  62.     }
  63.     
  64.     if(password != null && password.length() > 0) {
  65.         
  66.     
  67.     } else {
  68.         out.println("Password is need!<br/>");
  69.         regFlag = false;
  70.     
  71.     }
  72.     if(password2 != null && password2.length() > 0) {
  73.         
  74.         if( password2.equals(password)) {
  75.             
  76.         } else {
  77.         
  78.             out.println("Password2 is not equal Password!<br/>");
  79.             regFlag = false;
  80.         }
  81.     
  82.     } else {
  83.         out.println("Password2 is need!<br/>");
  84.         regFlag = false;
  85.     
  86.     
  87.     }
  88.     if (regFlag == true) {
  89.         application.setAttribute(username,username);            
  90.         
  91.         out.println("Register is  successful!");
  92.         
  93.     
  94.     }
  95. }
  96. %>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值