建站學院(LieHuo.Net)文檔 看了好久的ajax,自己動手做的例子卻很少,今天決定自己動手寫一個,功能很簡單,實現一個簡單的用戶名注冊功能,同時對用戶輸入在服務器端進行簡單校驗,沒有用數據庫保存用戶名,這里利用application·對象模擬。做了好久才弄好,遇到了很多問題,最后得以解決,比較有意思的地方是當我寫完例子后,在firefox下運行正常,但是在ie下有點問題,注冊過的用戶名還是顯示注冊成功,查了好久沒有搞出原因,最后又復習了以前看過的一個視頻教程,才知道是IE的緩存造成的,用了一個小技巧欺騙ie一下,就可以輕松搞定!感覺收獲挺大的!
下面是代碼:
注冊頁面html代碼:
提示:可修改后代碼再運行!
服務器端asp代碼:
以下為引用的內容: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String type = request.getParameter("type"); if (type.equals("username")) { String username = request.getParameter("username"); if (username != null && username.length() > 0) { if (application.getAttribute(username) != null) { out.println("Username is exist!"); } else { out.println("Username is OK!"); } } else { out.println("Username is need!"); } } else if(type.equals("password")) { String password = request.getParameter("password"); if(password != null && password.length() > 0) { out.println("password is ok"); } else { out.println("Password is need!"); } } else if (type.equals("password2")) { String password2 = request.getParameter("password2"); if(password2 != null && password2.length() > 0) { String password = request.getParameter("password"); if( password2.equals(password)) { out.println("Password2 is OK!"); } else { out.println("Password2 is not equal Password!"); } } else { out.println("Password2 is need!"); } } else if (type.equals("submit")) { boolean regFlag = true; String username = request.getParameter("username"); String password = request.getParameter("password"); String password2 = request.getParameter("password2"); if (username != null && username.length() > 0) { if (username != null && username.length() > 0) { if (application.getAttribute(username) != null) { regFlag = false; out.println("username is exist!"); } } } else { out.println("Username is needed!<br/>"); regFlag = false; } if(password != null && password.length() > 0) { } else { out.println("Password is need!<br/>"); regFlag = false; } if(password2 != null && password2.length() > 0) { if( password2.equals(password)) { } else { out.println("Password2 is not equal Password!<br/>"); regFlag = false; } } else { out.println("Password2 is need!<br/>"); regFlag = false; } if (regFlag == true) { application.setAttribute(username,username); out.println("Register is successful!"); } } %> |