博客
关于我
超市订单管理系统项目(2)——登录功能实现(后端)
阅读量:338 次
发布时间:2019-03-04

本文共 4869 字,大约阅读时间需要 16 分钟。

登录功能实现

  1. 登录页面login.jsp

  2. 设置欢迎页

    login.jsp

    或者在index.jsp中写

  3. dao

    public interface LoginDao {         //获取用户登录信息    public User getLogin(String userCode) throws SQLException;}
  4. daoimpl

    public class LoginDaoImpl implements LoginDao {         @Override    public User getLogin(String uCode) throws SQLException {             Connection connection = null;        Connection conn = BaseDao.getConn();//立即建立连接        PreparedStatement preparedStatement = null;        ResultSet resultSet = null;        User user = null;        if (conn != null) {     //只有当连接不为mull,即数据库打开时,才能操作各种有关数据库的语句            String sql = "select * from smbms_user where userCode=?";            Object[] params = {     uCode};            resultSet = BaseDao.executeQ(conn, preparedStatement, resultSet, sql, params);            if (resultSet.next()) {                     int id = resultSet.getInt("id");                String userCode = resultSet.getString("userCode");                String userName = resultSet.getString("userName");                String userPassword = resultSet.getString("userPassword");                int gender = resultSet.getInt("gender");                Date birthday = resultSet.getDate("birthday");                String phone = resultSet.getString("phone");                String address = resultSet.getString("address");                int userRole = resultSet.getInt("userRole");                int createdBy = resultSet.getInt("createdBy");                Date creationDate = resultSet.getDate("creationDate");                int modifyBy = resultSet.getInt("modifyBy");                Date modifyDate = resultSet.getDate("modifyDate");                int age = resultSet.getInt("age");                String userRoleName = resultSet.getString("userRoleName");                user = new User(id, userCode, userName, userPassword, gender, birthday, phone, address, userRole, createdBy, creationDate, modifyBy, modifyDate, age, userRoleName);            }            BaseDao.close(null, preparedStatement, resultSet);        }    return user;    }}
  5. service

    public interface LoginService {         //登录业务处理    public User login(String uCode,String uPassword) throws SQLException;}
  6. serviceimpl

    public class LoginServiceImpl implements LoginService {     //    private LoginDao loginDao;//    LoginServiceImpl(){     //        loginDao = new LoginDaoImpl();//    }    @Override    public User login(String uCode, String uPassword) throws SQLException {             LoginDao loginDao = new LoginDaoImpl();        User user = loginDao.getLogin(uCode);        return user;    }}
  7. servlet

    @WebServlet("/login.do")public class LoginServlet extends HttpServlet {      @Override    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {             String userCode = req.getParameter("userCode");        String password = req.getParameter("userPassword");        LoginService loginService = new LoginServiceImpl();        try {                 User user = loginService.login(userCode, password);            if (user != null){                     HttpSession session = req.getSession();                session.setAttribute(Constants.USER_SESSION, user);                resp.sendRedirect("jsp/frame.jsp");            }else {                     req.setAttribute("error", "用户名或密码错误");                req.getRequestDispatcher("login.jsp").forward(req, resp);            }        } catch (SQLException e) {                 e.printStackTrace();        }    }}

登录功能优化

实现注销:移除session,返回登录

//注销就是移除session@WebServlet("/user/exit.do")public class LogoutServlet extends HttpServlet {       @Override    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {           //创建session        HttpSession session = req.getSession();        //移除session        session.removeAttribute(Constants.USER_SESSION);        resp.sendRedirect("/login.jsp");    }}

登录拦截优化

编写filter

//拦截  未登录不能直接进入主页@WebFilter("/jsp/*")public class SysFilter implements Filter {       @Override    public void init(FilterConfig filterConfig) throws ServletException {       }    @Override    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {           HttpServletRequest request = (HttpServletRequest) servletRequest;        HttpServletResponse response = (HttpServletResponse) servletResponse;        HttpSession session = request.getSession();        User user_sess = (User) session.getAttribute(Constants.USER_SESSION);        if (user_sess == null){               request.getRequestDispatcher("/error.jsp").forward(request, response);        }else {               filterChain.doFilter(servletRequest, servletResponse);        }    }    @Override    public void destroy() {       }}

转载地址:http://ulgh.baihongyu.com/

你可能感兴趣的文章
MySQL 到底能不能放到 Docker 里跑?
查看>>
mysql 前缀索引 命令_11 | Mysql怎么给字符串字段加索引?
查看>>
mysql 协议的退出命令包及解析
查看>>
mysql 取表中分组之后最新一条数据 分组最新数据 分组取最新数据 分组数据 获取每个分类的最新数据
查看>>
mysql 四种存储引擎
查看>>
MySQL 基础模块的面试题总结
查看>>
MySQL 备份 Xtrabackup
查看>>
mysql 多个表关联查询查询时间长的问题
查看>>
mySQL 多个表求多个count
查看>>
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>