码迷,mamicode.com
首页 > 编程语言 > 详细

Java深入 - Filter过滤器

时间:2017-06-09 17:36:06      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:interface   filter过滤器   over   ros   for   exception   apach   placed   ons   

Java的1.3開始,对servlet2.3规范中增加了过滤器的支持。过滤器可以让我们对目标资源的请求和响应进行截取。

一些filter的特性:

1. Filter是Servlet规范的规定,须要Servlet容器的支持。

2. Filter不能使用Spring框架中的资源对象。

3. Filter仅仅在Servlet前后起作用。


Filter实现

我们须要实现接口Filter中定义的方法:

/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 *
 * You can obtain a copy of the license at
 * glassfish/bootstrap/legal/CDDLv1.0.txt or
 * https://glassfish.dev.java.net/public/CDDLv1.0.html.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * glassfish/bootstrap/legal/CDDLv1.0.txt.  If applicable,
 * add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your
 * own identifying information: Portions Copyright [yyyy]
 * [name of copyright owner]
 *
 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
 *
 * Portions Copyright Apache Software Foundation.
 */

package javax.servlet;

import java.io.IOException;


public interface Filter {

	public void init(FilterConfig filterConfig) throws ServletException;
	
	

    public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) 
throws IOException, ServletException;

	public void destroy();


}


自己定义一个TestFilter

/**
 * 自己定义一个Filter
 * @author zhuli
 * @date 2014-7-20
 */
public class TestFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("TestFiltern init"); //初始化容器的时候,这边会执行
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
throws IOException, ServletException {
        // TODO Auto-generated method stub
        HttpServletRequest request2 = (HttpServletRequest)request;
        String ip = IPUtil.getClientIp(request2);
        System.out.println("TestFiltern doFilter 请求前面就处理了 + ip: " + ip);
        chain.doFilter(request, response); //传递filter链
        System.out.println("业务处理完了之后,会继续调用这个filter,然后调用这边");
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        System.out.println("TestFiltern destroy"); //销毁容器的时候。这边会执行
    }

}

结果:

TestFiltern doFilter 请求前面就处理了 + ip: 127.0.0.1
业务逻辑=========
业务处理完了之后,会继续调用这个filter。然后调用这边


Filter配置

在web.xml中。配置servlet前面配置Filter就可以:

url-pattern能够配置符合哪些请求的须要走这个filter

	<!-- TEST filter -->
	<filter>
		<filter-name>testFilter</filter-name>
		<filter-class>com.xxx.test.TestFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>testFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


Filter的运行顺序

能够先看一张图:

技术分享

如果我们有两个过滤器。TestFilter和TestFilter1。

当web请求进来

->

我们运行TestFilter过滤器中chain.doFilter之前的代码(比如我们的样例中System.out.println("TestFiltern doFilter 请求前面就处理了");)

->

然后运行TestFilter2过滤器中chain.doFilter之前的代码

->

然后Servlet中的service方法

->

然后处理TestFilter2过滤器中chain.doFilter之后的代码

->

然后处理TestFilter过滤器中chain.doFilter之后的代码(比如我们的样例中System.out.println("业务处理完了之后。会继续调用这个filter。然后调用这边");)


Filter生命周期

1. init初始化。在web容器启动的时候。对Filter进行初始化。

初始化会调用init()方法

2. 过滤。详细过滤是doFilter()方法

3. destroy销毁。在容器关闭的时候会对Filter进行销毁。


Java深入 - Filter过滤器

标签:interface   filter过滤器   over   ros   for   exception   apach   placed   ons   

原文地址:http://www.cnblogs.com/wzzkaifa/p/6972655.html

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