码迷,mamicode.com
首页 > 其他好文 > 详细

Filter的执行顺序

时间:2018-02-23 18:58:43      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:for   contex   color   null   service   过滤   pre   一个   extend   

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

技术分享图片

二、测试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中配置的先后顺序

Filter的执行顺序

标签:for   contex   color   null   service   过滤   pre   一个   extend   

原文地址:https://www.cnblogs.com/happyflyingpig/p/8462883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!