JSP+Servlet实现登录功能

一、Dao层数据库操作

完美登录:需要进行条件判断,用户登录时,先查询该用户的账号,如果存在则再查询该用户的密码。如果不存在则提示账号不存在;如果存在且密码正确,则登录成功

用户账号与密码一起查询:

    /**
     * @param username
     * @param password
     * @return
     * @Author CodeSleep
     * @Date: 2021/7/20 10:48
     * @Description: //TODO 用户登录查询,查询用户账号、密码和账号状态
     */
    public UserEntity UserLogin(String username, String password) {
        String sql = "SELECT * FROM `user` WHERE username = ? AND userpassword = ?";
        List<UserEntity> list = (List<UserEntity>) DBUtil.exQuery(sql, UserEntity.class, username, password);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

二、Service业务层操作

    /**
     * @param username
     * @param password
     * @return
     * @Author CodeSleep
     * @Date: 2021/7/20 10:49
     * @Description: //TODO 用户登录业务层
     */
    public UserEntity UserLogin(String username, String password) {
        // 需要先做MD5加密处理 password => MD5加密
        password = MD5Util.getEncodeByMd5(password);
        return userDao.UserLogin(username, password);

    }

三、Colltroller操作

@WebServlet(name = "UserColltroller", urlPatterns = {"/user.do"})
public class UserColltroller extends HttpServlet {
    /**
     * Servlet要访问service
     */
    UserService userService = new UserService();

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

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 判断用户是否登录,不能登录则不能访问user.do
        if (username == null || password == null) {
            ResponseMessage.msg = "您无权访问,请先登录";
            request.setAttribute("msg", ResponseMessage.msg);
            // 回到登录页面重新登录,给出提示信息
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
        UserEntity user = userService.UserLogin(username, password);
        // 如果userEntity为null,则用户不存在
        if (user == null) {
            ResponseMessage.msg = "用户不存在或账号密码错误!";
            request.setAttribute("msg", ResponseMessage.msg);
            // 回到登录页面重新登录,给出提示信息
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else {
            if (user.getUserstatus() == 0) {
                ResponseMessage.msg = "该用户已被禁用,请联系管理员!";
                request.setAttribute("msg", ResponseMessage.msg);
                // 回到登录页面重新登录,给出提示信息
                request.getRequestDispatcher("login.jsp").forward(request, response);
            } else {
                // 1 需要将用户信息存储在session中,在servlet中获取session的方法
                // 将用户信息存储在Servlet->HttpSession jsp->session
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
                // 2 存储数据在客户端的cookie中
                Cookie usernameCookie = new Cookie("username", username);
                Cookie passwordCookie = new Cookie("password", password);
                // 3 设置生存时间
                usernameCookie.setMaxAge(60 * 60 * 24);
                passwordCookie.setMaxAge(60 * 60 * 24 * 7);
                // 4 存储到客户端
                response.addCookie(usernameCookie);
                response.addCookie(passwordCookie);
                //转发到news页面(后台首页)
                request.getRequestDispatcher("news.do?op=queryByPage").forward(request, response);
            }
        }
    }
}
最后修改:2021 年 07 月 28 日 04 : 02 PM
如果觉得我的文章对你有用,请随意赞赏