Goods:注册页面保存User功能发送邮件以及激活功实现

UserService

 1 // 激活功能更
 2     public void activation(String code) throws UserException {
 3         /*
 4          * 1通过激活码查询用户 2如果用户为Null说明是无效激活码 抛出异常 给出异常信息 3、查看用户状态为true
 5          */
 6 
 7         try {
 8             User user = userDao.findByCode(code);
 9             if (user == null)
10                 throw new UserException("无效的激活码");
11             if (user.isStatus())
12                 throw new UserException("您已经激活过了,不能二次激活!");
13             userDao.updateStatus(user.getUid(), true);
14         } catch (SQLException e) {
15             // TODO Auto-generated catch block
16             throw new RuntimeException(e);
17         }
18     }
19 
20     // 注册功能
21     public void regist(User user) {
22         // 1、数据的补齐
23         user.setUid(CommonUtils.uuid()); // commonUtils用来生成唯一的码
24         user.setStatus(false);
25         user.setActivationCode(CommonUtils.uuid() + CommonUtils.uuid());
26         // 2、项数据库中插入
27 
28         try {
29             userDao.add(user);
30         } catch (SQLException e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33         }
34         /*
35          * 3、发邮件
36          */
37         Properties prop = new Properties();
38         // 加载配置文件
39         try {
40             prop.load(this.getClass().getClassLoader()
41                     .getResourceAsStream("email_template.properties"));
42         } catch (IOException e1) {
43             throw new RuntimeException(e1);
44         }
45         // 登录邮件服务器得到session 发送mail对象
46         String host = prop.getProperty("host");// 服务器主机名
47         String username = prop.getProperty("username");// 登录名
48         String password = prop.getProperty("password");
49         Session session = MailUtils.createSession(host, username, password);
50 
51         // 创建mail对象
52         String from = prop.getProperty("from");
53         String to = user.getEmail();
54         String subject = prop.getProperty("subject"); // 主题
55         // messageformat用来替换占位符 pattern中的{0}{1}{2}占位符用第二个,第三个,第四个参数来替换
56         String content = MessageFormat.format(prop.getProperty("content"),
57                 user.getActivationCode());
58 
59         try {
60             MailUtils.send(session, new Mail(from, to, subject, content));
61         } catch (MessagingException e) {
62             throw new RuntimeException(e);
63         } catch (IOException e) {
64             throw new RuntimeException(e);
65         }
66 
67     }
View Code

UserDao

 1 /通过激活码查询用户
 2     public User findByCode(String code)
 3     {
 4         String sql="select * from t_user where activationCode=?";
 5         try {
 6             return qr.query(sql, new BeanHandler<User>(User.class),code);
 7         } catch (SQLException e) {
 8             // TODO Auto-generated catch block
 9             throw new RuntimeException(e);
10         }
11     }
12     //更新激活状态
13     public void updateStatus(String uid,boolean status) throws SQLException
14     {
15         String sql="update t_user set status=? where uid=?";
16         qr.update(sql,status,uid);
17         
18     }
View Code

UserServlet

 1 // 注册功能
 2     public String regist(HttpServletRequest req, HttpServletResponse resp)
 3             throws ServletException, IOException {
 4         // 1、封装表单数据到user对象中
 5         User formUser = CommonUtils.toBean(req.getParameterMap(), User.class);
 6 
 7         // 2、校验 如果校验 失败 保存错误信息返回到regist页面显示
 8 
 9         /*
10          * 调用validateRegist方法,检验Map是否为空 如果为空 则说明没有错误信息 否则
11          * 则有错误新信息,把map保存到request中 转发到regist.jsp
12          */
13         Map<String,String> errors= validateRegist(formUser, req.getSession());
14         if(errors.size()>0)
15         {  //错误信息的时候 把表单数据带回去
16             req.setAttribute("form", formUser);
17            req.setAttribute("errors", errors);
18         return "f:/jsps/user/regist.jsp";
19         }
20         // 3、使用service完成业务
21         userService.regist(formUser);
22 
23         // 4、保存成功信息转发到msg.jsp显示
24         req.setAttribute("code", "success");
25         req.setAttribute("msg", "注册成功,请马上到邮箱激活");
26         return "f:/jsps/msg.jsp"; // f代表转发 入request里面放有东西 BaseServlet实现的转发
27     }
28 
29     // 完成注册校验功能  对表单的字段进行逐个校验
30 
31     private Map<String, String> validateRegist(User formUser,HttpSession session) {
32         Map<String, String> errors = new HashMap<String, String>();
33         // 校验用户名
34         String loginname = formUser.getLoginname();
35         if (loginname == null || loginname.trim() == null)
36             errors.put("loginname", "用户名不能为空");
37         else if (loginname.length() < 3 || loginname.length() > 20)
38             errors.put("loginname", "用户名长度必须在3-20字符中");
39         else if (!userService.ajaxValidateLoginname(loginname))
40             errors.put("loginname", "用户名已被注册");
41         // 校验登录密码
42         String loginpass = formUser.getLoginpass();
43         if (loginpass == null || loginpass.trim() == null)
44             errors.put("loginpass", "密码不能为空");
45         else if (loginpass.length() < 3 || loginpass.length() > 20)
46             errors.put("loginpass", "密码长度必须在3-20字符中");
47         // 确认密码校验
48         String reloginpass = formUser.getReloginpass();
49         if (reloginpass == null || reloginpass.trim() == null)
50             errors.put("reloginpass", "密码不能为空");
51         else if (reloginpass.length() < 3 || reloginpass.length() > 20)
52             errors.put("reloginpass", "密码长度必须在3-20字符中");
53         else if(!reloginpass.equals(loginpass))
54             errors.put("reloginpass", ",两次输入密码不一致");
55         
56         //email校验
57         String email = formUser.getEmail();
58         if (email == null || email.trim() == null)
59             errors.put("email", "email不能为空");
60         else if (!email.matches("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$"))
61             errors.put("email", "email格式不正确");
62         else if (!userService.ajaxValidateEmail(email))
63             errors.put("email", "email已被注册");
64         
65         //验证码校验
66        String verifyCode=formUser.getVerifyCode();
67        String vCode=(String) session.getAttribute("vCode");
68        if (verifyCode == null || verifyCode.trim() == null)
69             errors.put("verifyCode", "验证码不能为空");
70         else if (!verifyCode.equalsIgnoreCase(vCode))
71             errors.put("verifyCode", "验证码错误!");
72        
73         return errors;
74         
75 
76     }
77 
78     // 激活功能
79     public String activation(HttpServletRequest req, HttpServletResponse resp) {
80         //System.out.println("activation");
81         //1、获取激活码 
82         //2、交给service 的 activation功能来激活
83         //service方法可能跑出异常 把异常信息拿来保存到request中
84         //转发到msg.jsp显示
85         String code=req.getParameter("activationCode");
86         try {
87             userService.activation(code);
88             req.setAttribute("code", "success");
89             req.setAttribute("msg", "恭喜激活成功,请马上登录!");
90         } catch (UserException e) {
91             req.setAttribute("msg", e.getMessage());
92             req.setAttribute("code", "error");
93             
94         }
95         return "f:/jsps/msg.jsp";
96     }
97 }
View Code

 

 

posted @ 2015-08-28 20:41  尾巴草  阅读(393)  评论(0编辑  收藏  举报