JavaWeb-SMBMS 登录流程解析

JavaWeb-SMBMS 登录流程解析

结构

在这里插入图片描述

数据库访问Dao层

  • 接口 UserDao : 定义了函数操作
/**
   根据 登录名 与 密码 来找到登录的用户
**/
public interface UserDao {
    //得到要登录的用户
    public User getLoginUser(Connection connection,String userCode,String password) throws SQLException;
}
  • UserDao实现类(UserDaoImpl):实现UserDao接口:
public class UserDaoImpl implements UserDao {
    @Override
    public User getLoginUser(Connection connection, String userCode,String password) throws SQLException {
        PreparedStatement preparedStatement = null;  //作为BaseDao.execute的参数
        ResultSet resultSet = null;//作为BaseDao.execute的参数
        User user = null;  //返回值
        if(connection!=null)
        {
            String sql = "select * from smbms_user where userCode=? and password=?";    //sql语句,根据userCode 和 password 来select
            Object params[] = {userCode,password};  //作为BaseDao.execute的参数
            //根据sql筛选
            resultSet = BaseDao.execute(connection,resultSet,preparedStatement,params,sql);
            if(resultSet.next())   //找到User并赋值
            {
                user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUserCode(resultSet.getString("userCode"));
                user.setUserName(resultSet.getString("userName"));
                user.setPassword(resultSet.getString("password"));
                user.setGender(resultSet.getString("gender"));
                user.setBirthday(resultSet.getDate("birthday"));
                user.setPhone(resultSet.getString("phone"));
                user.setAddress(resultSet.getString("address"));
                user.setUserRole(resultSet.getString("userRole"));
                user.setCreatedBy(resultSet.getString("createdBy"));
                user.setCreationDate(resultSet.getDate("creationDate"));
                user.setModifyBy(resultSet.getString("modifyBy"));
                user.setModifyDate(resultSet.getDate("modifyDate"));
            }
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return user;  //返回
    }
}

业务逻辑Service层

  • UserService 接口 :
    提供一个接口,规范 实现类的使用
public interface UserService {
    //用户登录
    public User login(String userCode, String password);
}
  • UserServiceImpl 接口:
    实现UserService接口,进行具体的业务操作
public class UserServiceImpl implements UserService{
//    业务层都会调用Dao层,所以要引入Dao层
    private UserDao userDao;
    public UserServiceImpl(){
        userDao = new UserDaoImpl();  //Dao层的注入是必要的
    }
    @Override
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;
        try{
            connection = BaseDao.getConnection();
            //通过业务层调用具体的数据库操作
            user = userDao.getLoginUser(connection,userCode,password);
        }catch (SQLException e)
        {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;  //返回找到的对象
    }
}

控制Servlet层:

public class LoginServlet extends HttpServlet {
    /**
     * 控制层,调用业务层代码
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet------start......");
        //获取用户名和密码
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");
        //和数据库中的密码进行对比,调用业务层
        UserService userService = new UserServiceImpl();
        User user = userService.login(userCode,userPassword);//已经把登录的人查出来了
        if(user!=null)
        {
            //查到了,将用户的信息放到Session当中,以后随时都可以在网站找到
            //(已登录用户的信息一般储存在Session中)
            req.getSession().setAttribute(Constants.USER_SESSION,user);
            //跳转到 主页
            resp.sendRedirect("jsp/frame.jsp");
        }else
        {
            //无法登录,转发会登录页面
            req.setAttribute("error","用户名/密码不正确");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值