• Filter系列教材 (一)- 过滤器 Filter 教程


    步骤1:Filter概念 
    步骤2:编写 FirstFilter
    步骤3:配置web.xml
    步骤4:访问页面
    步骤5:init() 方法
    步骤6:Filter启动失败

    步骤 1 : Filter概念 

    Filter就像一个一个哨卡,用户的请求需要经过Filter
    并且可以有多个过滤器

    Filter概念

    步骤 2 : 编写 FirstFilter

    开发一个简单的FirstFilter,用来打印用户访问ip地址和访问的页面

    HttpServletRequest request = (HttpServletRequest) req;


    doFilter()方法中的req参数的类型是ServletRequest,需要转换为HttpServletRequest类型方便调用某些方法 (参考request常见方法)

    String ip = request.getRemoteAddr();


    获取来路用户的ip地址

    String url = request.getRequestURL().toString();


    获取用户访问的页面地址

    System.out.printf("%s %s 访问了 %s%n", date, ip, url);


    在控制台打印出来

    chain.doFilter(request, response);


    过滤器放行,表示继续运行下一个过滤器,或者最终访问的某个servlet,jsp,html等等

    编写 FirstFilter

    package filter;

    import java.io.IOException;

    import java.text.SimpleDateFormat;

    import java.util.Date;

    import javax.servlet.Filter;

    import javax.servlet.FilterChain;

    import javax.servlet.FilterConfig;

    import javax.servlet.ServletException;

    import javax.servlet.ServletRequest;

    import javax.servlet.ServletResponse;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    public class FirstFilter implements Filter {

        @Override

        public void destroy() {

        }

        @Override

        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

                throws IOException, ServletException {

            HttpServletRequest request = (HttpServletRequest) req;

            HttpServletResponse response = (HttpServletResponse) res;

            String ip = request.getRemoteAddr();

            String url = request.getRequestURL().toString();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date d = new Date();

            String date = sdf.format(d);

            System.out.printf("%s %s 访问了 %s%n", date, ip, url);

            chain.doFilter(request, response);

        }

        @Override

        public void init(FilterConfig arg0) throws ServletException {

        }

    }

    步骤 3 : 配置web.xml

    在web.xml中进行filter的配置,和servlet的配置很类似
     

    <url-pattern>/*</url-pattern>


    表示所有的访问都会过滤

    如果配置成

    <url-pattern>*.jsp</url-pattern>


    就表示只过滤jsp

    <filter>

        <filter-name>FirstFilter</filter-name>

        <filter-class>filter.FirstFilter</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>FirstFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

    步骤 4 : 访问页面

    重启tomcat,所有用户的访问信息,都可以打印出来

    访问页面

    步骤 5 : init() 方法

    与Servlet需要配置自启动才会随着tomcat的启动而执行init()方法不一样。

    Filter一定会随着tomcat的启动自启动。

    init() 方法

    package filter;

    import java.io.IOException;

    import java.text.SimpleDateFormat;

    import java.util.Date;

    import javax.servlet.Filter;

    import javax.servlet.FilterChain;

    import javax.servlet.FilterConfig;

    import javax.servlet.ServletException;

    import javax.servlet.ServletRequest;

    import javax.servlet.ServletResponse;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    public class FirstFilter implements Filter {

        @Override

        public void destroy() {

        }

        @Override

        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

                throws IOException, ServletException {

            HttpServletRequest request = (HttpServletRequest) req;

            HttpServletResponse response = (HttpServletResponse) res;

            String ip = request.getRemoteAddr();

            String url = request.getRequestURL().toString();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date d = new Date();

            String date = sdf.format(d);

            System.out.printf("%s %s 访问了 %s%n", date, ip, url);

            chain.doFilter(request, response);

        }

        @Override

        public void init(FilterConfig arg0) throws ServletException {

            System.out.println("First Filter init()");

        }

    }

    步骤 6 : Filter启动失败

    Filter是web应用非常重要的一个环节,如果Filter启动失败,或者本身有编译错误,不仅这个Filter不能使用整个web应用会启动失败,导致用户无法访问页面

    在启动tomcat过程中,也会看到这样的字样: 
     

    严重: Context [] startup failed due to previous errors


    这常常用于提示Filter启动失败了

    Filter启动失败

    package filter;

    import java.io.IOException;

    import java.text.SimpleDateFormat;

    import java.util.Date;

    import javax.servlet.Filter;

    import javax.servlet.FilterChain;

    import javax.servlet.FilterConfig;

    import javax.servlet.ServletException;

    import javax.servlet.ServletRequest;

    import javax.servlet.ServletResponse;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    public class FirstFilter implements Filter {

        @Override

        public void destroy() {

        }

        @Override

        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

                throws IOException, ServletException {

            HttpServletRequest request = (HttpServletRequest) req;

            HttpServletResponse response = (HttpServletResponse) res;

            String ip = request.getRemoteAddr();

            String url = request.getRequestURL().toString();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date d = new Date();

            String date = sdf.format(d);

            System.out.printf("%s %s 访问了 %s%n", date, ip, url);

            chain.doFilter(request, response);

        }

        @Override

        public void init(FilterConfig arg0) throws ServletException {

            System.out.println("故意造成一个异常");

            Object o = null;

            o.toString();

        }

    }


    更多内容,点击了解: https://how2j.cn/k/filter/filter-tutorial/588.html

  • 相关阅读:
    十:Webpack 引入bootstrap
    九:Webpack结合ES6
    pfx格式密钥库修改密码
    邮件发送接收工具
    用keytool制作证书并在tomcat配置https服务(四)
    用keytool制作证书并在tomcat配置https服务(三)
    用keytool制作证书并在tomcat配置https服务(二 )
    用keytool制作证书并在tomcat配置https服务(一)
    java全角和半角转换
    RC4加密解密
  • 原文地址:https://www.cnblogs.com/Lanht/p/12789441.html
Copyright © 2020-2023  润新知