博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java深入 - Filter过滤器
阅读量:7012 次
发布时间:2019-06-28

本文共 3177 字,大约阅读时间需要 10 分钟。

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

testFilter
com.xxx.test.TestFilter
testFilter
/*

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进行销毁。

你可能感兴趣的文章
Android之图片
查看>>
Edittext 各种属性
查看>>
Cisco BGP后门路由解析
查看>>
NDK问题 之一: Javah 生成.h文件
查看>>
Kafka 源码剖析(一)
查看>>
cocos2d-x游戏例子01:是男人就坚持20秒(WIN32)
查看>>
初识html5 File API实现带有进度提示的文件上传
查看>>
使用 Chrome 浏览器调试 WebView 中的网页
查看>>
Linux下制作U盘系统启动盘的方法
查看>>
Torando 参数配置
查看>>
Mysql找回root密码
查看>>
一小时包教会 —— webpack 入门指南
查看>>
linux配置cvs
查看>>
JDK
查看>>
centos 用crt登陆后提示信息
查看>>
wireshark抓包图解-tcp三次握手四次挥手详解/
查看>>
从构建分布式秒杀系统聊聊分布式锁
查看>>
mysql多实例安装脚本
查看>>
数据库mysql 分库备份脚本
查看>>
Cocos2d-x3.2 Progress进度条
查看>>