24. 过滤器使用

2020-11-25  本文已影响0人  卡尔书院

登录: 用户登录之后才能进入主页!用户注销后就不能进入主页了!

  1. 用户登录之后,向Sesison中放入用户的数据

    package com.karl.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class LoginServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req, resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取前端请求的参数
            String username = req.getParameter("username");
            if (username.equals("admin")){//登录成功
                req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
                resp.sendRedirect(req.getContextPath()+"/sys/success.jsp");
            }else {
                resp.sendRedirect(req.getContextPath()+"/error.jsp");
            }
        }
    }
    
  2. i进入主页的时候要判断用户是否已经登录; 要求:在过滤器中实现!

    package com.karl.filter;
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    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 servletReq = (HttpServletRequest)servletRequest;
            HttpServletResponse servletResp = (HttpServletResponse) servletResponse;
    
            if(null == servletReq.getSession().getAttribute("USER_SESSION")){
                servletResp.sendRedirect(servletReq.getContextPath()+"/error.jsp");
            }
    
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    
  3. 注销

    package com.karl.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.HashMap;
    
    public class LogoutServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Object user_session = req.getSession().getAttribute("USER_SESSION");
            if (null != user_session){
                req.getSession().removeAttribute("USER_SESSION");
                resp.sendRedirect(req.getContextPath()+"/index.jsp");
            }else {
                resp.sendRedirect(req.getContextPath()+"/index.jsp");
            }
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
上一篇下一篇

猜你喜欢

热点阅读