首页 > 代码库 > 注册模块前端的处理及检查用户的唯一性

注册模块前端的处理及检查用户的唯一性

用户名和密码为空,不允许注册

用户重复,给出提示,不允许注册

否则可以注册

需要用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,不存在:0
17                 $("[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

 

注册模块前端的处理及检查用户的唯一性