22-登录功能实现

22-登录功能实现

概述

本文主要讲通过编写servlet,service,dao层,实现一个完整的登录功能
dao层负责从数据库查询数据,service层负责创建资源,关闭资源,servlet层负责验证用户,负责页面重定向

遇到的坑

使用Tomacat启动会遇到无法加载mysql驱动的情况,需要在idea只能怪进行设置File->Project Structure->Artifacts->WEB-INF下面创建lib目录将所有的依赖移动进去

实践

UserDao.java


package com.kuang.dao.user;

import com.kuang.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 功能描述
 *
 * @since 2022-09-05
 */
public interface UserDao {
    public User getLoginUser(Connection conn, PreparedStatement preparedStatement, ResultSet resultSet, String sql,
        Object[] params);
}

UserDaoImpl.java


package com.kuang.dao.user;

import com.kuang.dao.BaseDao;
import com.kuang.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 功能描述
 *
 * @since 2022-09-05
 */
public class UserDaoImpl implements UserDao {
    @Override
    public User getLoginUser(Connection conn, PreparedStatement preparedStatement, ResultSet resultSet, String sql,
        Object[] params) {
        User user = new User();
        try {
            resultSet = BaseDao.execute(conn, sql, params, preparedStatement, resultSet);
            if (resultSet.next()) {
                user.setAddress(resultSet.getString("address"));
                user.setBirthday(resultSet.getDate("birthday"));
                user.setUserCode(resultSet.getString("userCode"));
                user.setUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userPassword"));
                user.setUserRole(resultSet.getString("userRole"));
                user.setCreatedBy(resultSet.getInt("createdBy"));
                user.setCreationDate(resultSet.getDate("creationDate"));
                user.setModifyBy(resultSet.getInt("modifyBy"));
                user.setModifyDate(resultSet.getDate("modifyDate"));
                user.setGender(resultSet.getInt("gender"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }
}

UserService.java


package com.kuang.service;

import com.kuang.pojo.User;

/**
 * 功能描述
 *
 * @since 2022-09-05
 */
public interface UserService {
    public User getLoginUser(String UserCode);
}

UserServiceImpl.java


package com.kuang.service;

import com.kuang.dao.BaseDao;
import com.kuang.dao.user.UserDao;
import com.kuang.dao.user.UserDaoImpl;
import com.kuang.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 功能描述
 *
 * @since 2022-09-05
 */
public class UserServiceImpl implements UserService {
    private Connection connection = BaseDao.getConnection();

    private UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public User getLoginUser(String UserCode) {
        String sql = "select * from smbms_user where userCode=?";
        Object[] params = {UserCode};
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        User loginUser = userDao.getLoginUser(connection, preparedStatement, resultSet, sql, params);
        BaseDao.closeResource(null, preparedStatement, resultSet);
        return loginUser;
    }
}

LoginServlet.java


package com.kuang.servlet;

import com.kuang.pojo.User;
import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;
import com.kuang.utils.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能描述
 *
 * @since 2022-09-05
 */
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 4928727484940769433L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");
        UserService userService = new UserServiceImpl();
        User user = userService.getLoginUser(userCode);
        if (user == null) {
            System.out.println("登录失败1");
            resp.sendRedirect("/smbms/error.jsp");
        } else {
            if (user.getUserPassword().equals(userPassword)) {
                // 将用户信息保存到Session中
                System.out.println("登录成功");
                req.getSession().setAttribute(Constants.USER_SESSION, user);
                resp.sendRedirect("/smbms/jsp/frame.jsp");
            } else {
                System.out.println("登录失败2");
                resp.sendRedirect("/smbms/error.jsp");
            }
        }
    }

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

Constants.java


package com.kuang.utils;

/**
 * 功能描述
 *
 * @since 2022-09-05
 */
public class Constants {
    public static final String USER_SESSION = "UserSession";
}

注册servlet web.xml

    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>com.kuang.servlet.LoginServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login.do</url-pattern>
    </servlet-mapping>
posted @ 2022-09-06 20:36  Oh,mydream!  阅读(61)  评论(0编辑  收藏  举报