登录功能-分析、实现

登录功能分析

登录功能实现

 LoginServlet类:

复制代码
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取验证码
        String check = request.getParameter("check");
        // 从session中获取验证码
        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        // 保证验证码只能使用一次
        session.removeAttribute("CHECKCODE_SERVER");
        // 判断
        if (checkcode_server == null && !checkcode_server.equalsIgnoreCase(check)){
            // 验证码错误
            ResultInfo info = new ResultInfo();
            info.setFlag(false);
            info.setErrorMsg("验证码错误");
            // 将info对象序列化为json
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(info);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(json);
            return;
        }


        // 1、获取用户名和密码数据
        Map<String, String[]> map = request.getParameterMap();
        // 2、封装User对象
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        // 3、调用Service查询
        UserService service = new UserServiceImpl();
        User u = service.login(user);
     session.setAttribute("user",u);
        ResultInfo info = new ResultInfo();
        // 4、判断用户对象是否存在
        if(u==null){
            //用户名密码错误
            info.setFlag(false);
            info.setErrorMsg("用户名或密码错误");
        }
        // 5、判断用户是否激活
        if(u != null && !"Y".equals(u.getStatus())){
            //用户尚未激活
            info.setFlag(false);
            info.setErrorMsg("您尚未激活,请激活");
        }
        // 6、判断登录成功
        if (u!=null && "Y".equals(u.getStatus())){
            info.setFlag(true);
        }

        // 响应数据
        ObjectMapper mapper = new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");
        mapper.writeValue(response.getOutputStream(),info);
    }

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

UserService接口:

User login(User user);

UserServiceImpl实现类:

复制代码
/**
     * 登录方法
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        return userDao.findByUsernameAndPassword(user.getUsername(),user.getPassword());
    }
复制代码

UserDao接口:

User findByUsernameAndPassword(String username, String password);

UserDaoImpl实现类:

复制代码
@Override
    public User findByUsernameAndPassword(String username, String password) {
        User user = null;
        try {
            // 定义sql
            String sql = "select * from tab_user where username = ? and password = ?";
            // 执行sql
            user = template.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),username,password);
        } catch (DataAccessException e) {

        }
        return user;
    }
复制代码

 

posted @ 2023-02-09 10:18  想见玺1面  阅读(24)  评论(0编辑  收藏  举报