Filter的执行顺序

一、 过滤器不仅能处理请求还能处理响应

二、测试Filter的执行顺序

  2.1.先写一个EncodingFilter

package org.burning.sport.filter;
import javax.servlet.*;
import java.io.IOException;

public class EncodingFilter implements Filter{
    private String encoding;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init EncodingFilter.......");
        encoding = filterConfig.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("before EncodingFilter doFilter.....");
        request.setCharacterEncoding(encoding);
        chain.doFilter(request, response);
        System.out.println("after EncodingFilter doFilter......");
    }

    @Override
    public void destroy() {
        System.out.println("destory EncodingFilter......");
    }
}

  2.2.写一个LoggerFilter

package org.burning.sport.filter;

import javax.servlet.*;
import java.io.IOException;

public class LoggerFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init LoggerFilter....");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String contextPath = request.getServletContext().getContextPath();
        System.out.println("LoggerFilter before doFilter.....");
        chain.doFilter(request, response);
        System.out.println("LoggerFilter after doFilter");
    }

    @Override
    public void destroy() {
        System.out.println("destory LoggerFilter....");
    }
}

  2.3 写一个HelloWorldServlet

package org.burning.sport.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloWorldServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("do service...." + this.getInitParameter("encoding"));
        super.service(req, resp);
    }
}

  2.4 配置web.xml

  filter的配置顺序跟执行顺序有关系

<filter>
    <filter-name>myEncodingFilter</filter-name>
    <filter-class>org.burning.sport.filter.EncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>myEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>myLoggerFilter</filter-name>
    <filter-class>org.burning.sport.filter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>myLoggerFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>MyHelloworldServlet</servlet-name>
    <servlet-class>org.burning.sport.filter.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyHelloworldServlet</servlet-name>
    <url-pattern>/helloServlet</url-pattern>
</servlet-mapping>

  2.5 看执行结果

启动tomcat的时候
init EncodingFilter.......
init LoggerFilter....
-----------------------------------------
before EncodingFilter doFilter.....
LoggerFilter before doFilter.....
do service....null
LoggerFilter after doFilter
after EncodingFilter doFilter......
---------------------------------------
销毁tomcat的时候
destory EncodingFilter......
destory LoggerFilter.... 

  2.6.结论:
  容器只会初始化一次
  filter的执行是有先后顺序的,根据在web.xml中配置的先后顺序

posted @ 2018-02-23 17:28  寻找风口的猪  阅读(4297)  评论(0编辑  收藏  举报