注册的后台以及邮件激活

注册的后台以及邮件激活

用户点击超链接激活邮箱 ,发送activeUserServlet(包含一个独一激活码)–>Servlet,
Servlet中
1.获取激活码
2.判断是否有激活码
3.通过激活码来查询User对象
4.判断对象是否存在
用户存在–>Service,service来调用Dao updateStatus(User user)

后台Servlet

@WebServlet("/activeUserServlet")
public class ActiveUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取激活码
        String code = request.getParameter("code");
        if (code != null) {
            UserService service = new UserServiceImpl();
            boolean flag = service.active(code);
            String msg = null;
            if (flag) {
               msg = "激活成功,请点击 登录 "; //应该跳转到相关的成功界面
            } else {
                msg = "激活失败,你自己的原因";
            }
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(msg);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

后台Service

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public boolean regist(User user) {
        User byUsername = userDao.findByUsername(user.getUsername());
        if (byUsername != null) {
            return false;//用户名已经存在
        }
        //保存用户信息,设置唯一字符串为激活码UUid
        user.setCode(UuidUtil.getUuid());
        user.setStatus("N");
        userDao.save(user);

        String content = " 点击激活 ";
        MailUtils.sendMail(user.getEmail(), content, "激活邮件");
        return true;

    }


    @Override
    public boolean active(String code) {
        //根据激活码来查询用户
        User user = userDao.findByCode(code);
        if (user != null) {
            userDao.upDataStatus(user);
            return true;
        } else {
            return false;

        }
    }

    /**
     * 用户点击超链接激活邮箱 ,发送activeUserServlet(包含一个独一激活码)-->Servlet,
     * Servlet中1.获取激活码 2.判断是否有激活码 3.通过激活码来查询User对象 4.判断对象是否存在
     * 用户存在-->Service,service来调用Dao
     * Dao updateStatus(User user)
     */

后台Dao

@Override
    public User findByCode(String code) {
        User user =null;
        try {
            String sql = "select * from tab_user where code = ?";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), code);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public void upDataStatus(User user) {
        String sql = "update tab_user set status = 'Y' where uid =?";
        template.update(sql,user.getUid());

    }


今天有点累,就学习了一点内容。总结一下
1.前台–>Servlet–>Service–>Dao
2.Servlet回写数据(dopost request()),必须要要设定request的字符集

response.setContentType("text/html;charset=utf-8");
response.getWriter().write(msg);

你可能感兴趣的:(项目实战)