博客
关于我
超市订单管理系统项目(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一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>