博客
关于我
超市订单管理系统项目(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/

你可能感兴趣的文章
NIO Selector实现原理
查看>>
nio 中channel和buffer的基本使用
查看>>
NIO_通道之间传输数据
查看>>
NIO三大组件基础知识
查看>>
NIO与零拷贝和AIO
查看>>
NIO同步网络编程
查看>>
NIO基于UDP协议的网络编程
查看>>
NIO笔记---上
查看>>
NIO蔚来 面试——IP地址你了解多少?
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NISP国家信息安全水平考试,收藏这一篇就够了
查看>>
NIS服务器的配置过程
查看>>
NIS认证管理域中的用户
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 模型中的偏差和公平性检测
查看>>