註冊模組前端的處理及檢查使用者的唯一性,模組前端檢查使用者

來源:互聯網
上載者:User

註冊模組前端的處理及檢查使用者的唯一性,模組前端檢查使用者

使用者名稱和密碼為空白,不允許註冊

使用者重複,給出提示,不允許註冊

否則可以註冊

需要用jQuery,因為jQuery跨瀏覽器,相容性好。

使用者註冊頁面(register.jsp)驗證:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset==UTF-8"> 7 <title>Insert title here</title> 8 <script type="text/javascript" src="resources/js/lib/jquery.min.js"></script> 9 <script type="text/javascript">10     $(function(){11         $("[name='userName']").blur(function(){12             if($.trim($(this).val()) == ""){13                 return;14             }15             $.post("checkExists.do",{userName:$(this).val(),pwd:""},function(data){16                 //已經存在:1,不存在:017                 $("[name='isExists']").val(data);18                 if(data == "1"){19                     $("#msg").html("該使用者名稱已經註冊,請重新輸入使用者名稱!");20                 }21             });22         });23         $("[type='submit']").click(function(){24             if($.trim($("[name='userName']").val()) == ""){25                 $("#msg").html("使用者名稱不可為空,請重新輸入使用者名稱!");26                 return false;27             }28             if($("[name='isExists']").val() == "1"){29                 $("#msg").html("該使用者名稱已經註冊,請重新輸入!");30                 return false;31             }32             if($.trim($("[name='pwd']").val()) == ""){33                 $("#msg").html("密碼不可為空,請輸入密碼!");34                 return false;35             }36             return true;37         });38     });39 </script>40 </head>41 <body>42 使用者註冊<br/>43 44 <form method="post" action="addUser.do">45     <table>46         <tr>47             <td>使用者名稱:</td>48             <td><input type="text" name="userName"/></td>49         </tr>50         <tr>51             <td>密碼:</td>52             <td><input type="password" name="pwd"/></td>53         </tr>54         <tr>55             <td colspan="2">56                 <input type="submit" value="提 交"/>57             </td>58         </tr>59     </table>60     <input type="hidden" name="isExists" value="0"/>61 </form>62 <div id="msg"></div>63 <br/>64 <br/>65 <a href="main.jsp">返回首頁面</a>66 </body>67 </html>
View Code

DAO 驗證使用者代碼:

 1 /** 2      * 檢查使用者是否存在 3      * @param user 4      * @return 5      */ 6     public boolean checkExists(User user){ 7         boolean rel = false; 8         String sql = "select id,username,pwd from users where username = ?"; 9         Connection conn = DBLib.getConn();10         PreparedStatement pstmt;11         try {12             pstmt = conn.prepareStatement(sql);13             pstmt.setString(1, user.getUserName());14             ResultSet set = pstmt.executeQuery();15             if(set.next()){16                 rel = true;17             }18             set.close();19             pstmt.close();20         } catch (SQLException e) {21             e.printStackTrace();22         } finally{23             try {24                 conn.close();25             } catch (SQLException e) {26                 e.printStackTrace();27             }28         }29         return rel;30     }
View Code

CheckExistsAction(一定要繼承Action類):

 1 package action; 2  3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5  6 import org.apache.struts.action.Action; 7 import org.apache.struts.action.ActionForm; 8 import org.apache.struts.action.ActionForward; 9 import org.apache.struts.action.ActionMapping;10 11 import dao.UserDAO;12 import myuser.User;13 14 public class CheckExistsAction extends Action {15 16     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,17             HttpServletResponse response) throws Exception {18         User user = (User)form;19         UserDAO dao = new UserDAO();20         String rel = dao.checkExists(user) ? "1" : "0";21         response.getWriter().write(rel);22         return null;23     }24 25 }
View Code

struts-config.xml配置:

1 <action path="/checkExists" type="action.CheckExistsAction" name="user"/>
View Code

原始碼連結:http://pan.baidu.com/s/1hrJISV6 提取碼:fjxk

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.