zoukankan      html  css  js  c++  java
  • JavaWeb-filter

    (一)过滤器Filter

    1.filter的简介

    filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目 标资源访问前后进行逻辑处理

    2.快速入门

    步骤:

    1)编写一个过滤器的类实现Filter接口

    2)实现接口中尚未实现的方法(着重实现doFilter方法)

    3)在web.xml中进行配置(主要是配置要对哪些资源进行过滤)

    3.Filter的API详解

    (1)filter生命周期及其与生命周期相关的方法

    Filter接口有三个方法,并且这个三个都是与Filter的生命相关的方法

    init(Filterconfig):代表filter对象初始化方法 filter对象创建时执行

    doFilter(ServletRequest,ServletResponse,FilterCha):代表filter执行过滤的核心方法,如果某资源在已经被配置到这个filter进行过滤的话,那么每次访问这个资源都会执行doFilter方法

    destory():代表是filter销毁方法 当filter对象销毁时执行该方法

    Filter对象的生命周期:

    Filter何时创建:服务器启动时就创建该filter对象

    Filter何时销毁:服务器关闭时filter销毁

    (2)Filter的AP详解

    1)init(FilterConfig)

    其中参数config代表 该Filter对象的配置信息的对象,内部封装是该filter的配置信息。


     

    2)destory()方法

    filter对象销毁时执行

    3)doFilter方法

    doFilter(ServletRequest,ServletResponse,FilterChain)

    其中的参数:

    ServletRequest/ServletResponse:每次在执行doFilter方法时 web容器负责创建一个request和一个response对象作为doFilter的参数传递进来。该request个该response就是在访问目标资源的service方法时的request和response。

    FilterChain:过滤器链对象,通过该对象的doFilter方法可以放行该请求


    4.Filter的配置


    url-pattern配置时

    1)完全匹配  /sertvle1

    2)目录匹配  /aaa/bbb/* ----最多的

    /user/*:访问前台的资源进入此过滤器

    /admin/*:访问后台的资源时执行此过滤器

    3)扩展名匹配  *.abc  *.jsp

     

    注意:url-pattern可以使用servlet-name替代,也可以混用

     

     

    dispatcher:访问的方式(了解)

    REQUEST:默认值,代表直接访问某个资源时执行filter

    FORWARD:转发时才执行filter

    INCLUDE: 包含资源时执行filter

    ERROR:发生错误时 进行跳转是执行filter

    总结Filter的作用?

    1)公共代码的提取

    2)可以对request和response中的方法进行增强(装饰者模式/动态代理)

    3)进行权限控制

    (二) 网站的自动登陆

    2.1 分析

     

     2.2 代码

    @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            
            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse resp = (HttpServletResponse) response;
            HttpSession session = req.getSession();
            
            //获得cookie中用户名和密码 进行登录的操作
            //定义cookie_username
            String cookie_username = null;
            //定义cookie_password
            String cookie_password = null;
            //获得cookie
            Cookie[] cookies = req.getCookies();
            if(cookies!=null){
                for(Cookie cookie : cookies){
                    //获得名字是cookie_username和cookie_password
                    if("cookie_username".equals(cookie.getName())){
                        cookie_username = cookie.getValue();
                        //恢复中文用户名
                        cookie_username = URLDecoder.decode(cookie_username, "UTF-8");
                    }
                    if("cookie_password".equals(cookie.getName())){
                        cookie_password = cookie.getValue();
                    }
                }
            }
            
            //判断username和password是否是null
            if(cookie_username!=null&&cookie_password!=null){
                //登录的代码
                UserService service = new UserService();
                User user = null;
                try {
                    user = service.login(cookie_username,cookie_password);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                //将登录的用户的user对象存到session中
                session.setAttribute("user", user);
            }
            
            //放行
            chain.doFilter(req, resp);
            
        }

    (三)全局解决乱码

    这里用的是装饰者模式来增强方法

    @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            
            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse resp = (HttpServletResponse) response;
            HttpSession session = req.getSession();
            
            //获得cookie中用户名和密码 进行登录的操作
            //定义cookie_username
            String cookie_username = null;
            //定义cookie_password
            String cookie_password = null;
            //获得cookie
            Cookie[] cookies = req.getCookies();
            if(cookies!=null){
                for(Cookie cookie : cookies){
                    //获得名字是cookie_username和cookie_password
                    if("cookie_username".equals(cookie.getName())){
                        cookie_username = cookie.getValue();
                        //恢复中文用户名
                        cookie_username = URLDecoder.decode(cookie_username, "UTF-8");
                    }
                    if("cookie_password".equals(cookie.getName())){
                        cookie_password = cookie.getValue();
                    }
                }
            }
            
            //判断username和password是否是null
            if(cookie_username!=null&&cookie_password!=null){
                //登录的代码
                UserService service = new UserService();
                User user = null;
                try {
                    user = service.login(cookie_username,cookie_password);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                //将登录的用户的user对象存到session中
                session.setAttribute("user", user);
            }
            
            //放行
            chain.doFilter(req, resp);
            
        }

    (四) 源码

    链接:https://pan.baidu.com/s/1Bvrk4GTPfEASnswIgbMODw 密码:g44f

     

     

     

     

     

     

     


    作者:8亩田
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.

    本文如对您有帮助,还请多帮 【推荐】 下此文。
    如果喜欢我的文章,请关注我的公众号
    如果有疑问,请下面留言

    学而不思则罔 思而不学则殆
  • 相关阅读:
    如何把项目中经常使用的信息放在全局的对象里,随取随用?
    优秀代码
    gcc编译C代码后,输出乱码
    mybatis !=null的一个坑
    String转int[]
    插值算法的公式 mid=low+(key-a[low])/(a[high]-a[low])*(high-low) 是怎么来的
    关于Leetcode的交替打印FooBar,我的答案一直超时
    git找回前几个版本删除的某个文件
    Google 此手机号无法用于验证 解决方法
    Postgresql 一对多如何将原本用,隔开的id替换为name
  • 原文地址:https://www.cnblogs.com/liu-wang/p/8636399.html
Copyright © 2011-2022 走看看