博客
关于我
超市订单管理系统项目(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 order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>