Filter技术详解

概述

在这里插入图片描述

应用:

  1. 全站乱码处理
  2. 30天自动登录

Idea中创建Filter

在这里插入图片描述

Filter中的方法介绍

在这里插入图片描述

责任链模式

在这里插入图片描述

Filter的生命周期

在这里插入图片描述

应用案例

EasyMall修改—全站乱码处理

  1. 新建encodefilter来拦截请求和响应(执行乱码处理以及动态字符集获取)

在这里插入图片描述

@WebFilter(filterName = "EncodingFilter",value = "/*")
public class EncodingFilter implements Filter {
    //属性---扩大使用范围
    String encode=null;
    public void destroy() {
    }
    //每次拦截执行的方法
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 
            throws ServletException, IOException {
        req.setCharacterEncoding(encode);
        resp.setContentType("text/html;charset="+encode);
        chain.doFilter(req, resp);//执行完乱码问题进行放行
    }
    //初始化默认调用的方法
    public void init(FilterConfig config) throws ServletException {
        //获取ServletContext对象
        ServletContext context = config.getServletContext();
        //获取全局配置信息---码表值
        String use_encode = context.getInitParameter("encode");
        encode=use_encode;
    }
}

  1. 配置web.xml来配置全局配置信息
    在这里插入图片描述

EasyMall修改—30天自动登录

1. 修改LoginServlet(设置Cookie)

在这里插入图片描述

2. 创建LoginFilter(完成30天自动登录)

在这里插入图片描述

@WebFilter(filterName = "LoginFilter",value = "/*")
public class LoginFilter implements Filter {
    public void destroy() {
    }
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 
            throws ServletException, IOException {

        //获取session对象
        HttpServletRequest request =(HttpServletRequest) req;//向下造型
        HttpSession session = request.getSession(false);

        //判断用户是否已登录
        if(session==null||session.getAttribute("user")==null){
            //用户未登录
            //需要登录但是需要根据用户名和密码才能登录
            //获取Cookie对象的信息(用户名和密码)
            Cookie[] cookies = request.getCookies();
            //扩大使用范围
            Cookie ck=null;
            //第一次请求没有Cookie
            if(cookies!=null){
                //遍历数组
                for(Cookie c:cookies){
                    //判断数组元素中是否有指定的Cookie对象
                    if("autologin".equals(c.getName())){
                        //获取指定的Cookie对象
                        ck=c;
                    }
                }
            }
            //判断是否找到指定的Cookie对象
            if(ck!=null){
                //获取指定Cookie对象中的用户名和密码
                //获取用户名
                String username= URLDecoder.decode(ck.getValue().split("#")[0],"utf8");
                //获取密码
                String password=ck.getValue().split("#")[1];
                //根据获取用户名和密码来做登录
                //调用service层方法来完成登录
                UserService userService=new UserService();
                try {
                    User user = userService.loginUser(username, password);
                    //登录成功保留用户登录状态
                    HttpSession ss = request.getSession();
                    //设置域属性
                    ss.setAttribute("user",user);
                } catch (MsgException e) {
                    //不做任何操作
                }
            }
        }
        //放行(request)
        chain.doFilter(request, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }
}

3. 修改LogOutServlet(删除30天自动登录的Cookie)

在这里插入图片描述

@WebServlet("/servlet/LogOutServlet")
public class LogOutServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        //登出---销毁session对象---自杀
        request.getSession().invalidate();
        //删除30天自动登录Cookie
        Cookie cookie=new Cookie("autologin","");
        cookie.setPath(request.getContextPath()+"/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        //跳转到首页
        response.sendRedirect("/"+request.getContextPath());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sparky*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值