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

你可能感兴趣的文章
NIFI从MySql中离线读取数据再导入到MySql中_无分页功能_02_转换数据_分割数据_提取JSON数据_替换拼接SQL_添加分页---大数据之Nifi工作笔记0037
查看>>
NIFI从PostGresql中离线读取数据再导入到MySql中_带有数据分页获取功能_不带分页不能用_NIFI资料太少了---大数据之Nifi工作笔记0039
查看>>
nifi使用过程-常见问题-以及入门总结---大数据之Nifi工作笔记0012
查看>>
NIFI分页获取Mysql数据_导入到Hbase中_并可通过phoenix客户端查询_含金量很高的一篇_搞了好久_实际操作05---大数据之Nifi工作笔记0045
查看>>
NIFI分页获取Postgresql数据到Hbase中_实际操作---大数据之Nifi工作笔记0049
查看>>
NIFI同步MySql数据_到SqlServer_错误_驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server_Navicat连接SqlServer---大数据之Nifi工作笔记0047
查看>>
NIFI同步MySql数据源数据_到原始库hbase_同时对数据进行实时分析处理_同步到清洗库_实际操作06---大数据之Nifi工作笔记0046
查看>>
Nifi同步过程中报错create_time字段找不到_实际目标表和源表中没有这个字段---大数据之Nifi工作笔记0066
查看>>
NIFI大数据进阶_FlowFile拓扑_对FlowFile内容和属性的修改删除添加_介绍和描述_以及实际操作---大数据之Nifi工作笔记0023
查看>>
NIFI大数据进阶_FlowFile生成器_GenerateFlowFile处理器_ReplaceText处理器_处理器介绍_处理过程说明---大数据之Nifi工作笔记0019
查看>>
NIFI大数据进阶_FlowFile生成器_GenerateFlowFile处理器_ReplaceText处理器_实际操作---大数据之Nifi工作笔记0020
查看>>
NIFI大数据进阶_Json内容转换为Hive支持的文本格式_实际操作_02---大数据之Nifi工作笔记0032
查看>>
NIFI大数据进阶_Json内容转换为Hive支持的文本格式_操作方法说明_01_EvaluteJsonPath处理器---大数据之Nifi工作笔记0031
查看>>
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka消费者处理器_来消费kafka数据---大数据之Nifi工作笔记0037
查看>>
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka生产者---大数据之Nifi工作笔记0036
查看>>
NIFI大数据进阶_NIFI的模板和组的使用-介绍和实际操作_创建组_嵌套组_模板创建下载_导入---大数据之Nifi工作笔记0022
查看>>
NIFI大数据进阶_NIFI监控功能实际操作_Summary查看系统和处理器运行情况_viewDataProvenance查看_---大数据之Nifi工作笔记0026
查看>>
NIFI大数据进阶_NIFI监控的强大功能介绍_处理器面板_进程组面板_summary监控_data_provenance事件源---大数据之Nifi工作笔记0025
查看>>
NIFI大数据进阶_NIFI集群知识点_认识NIFI集群以及集群的组成部分---大数据之Nifi工作笔记0014
查看>>
NIFI大数据进阶_NIFI集群知识点_集群的断开_重连_退役_卸载_总结---大数据之Nifi工作笔记0018
查看>>