Filter过滤器简介

  ServletAPI中提供了一个Filter接口,开发web应用时,如果编写的 java 类实现了这个接口,则把这个java类称之为过滤器Filter。

  WEB服务器每次在调用web资源的service方法之前(服务器内部对资源的访问机制决定的),都会先调用一下filter的doFilter方法。

  通过Filter技术,开发人员可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截。简单说,就是可以实现web容器对某资源的访问前截获进行相关的处理,还可以在某资源向web容器返回响应前进行截获进行处理

Filter的生命周期

  和Servlet一样,Filter的生命周期也是由Web服务器来负责的。

  实例化-->初始化-->服务-->销毁

  和Servlet生命周期的区别:

  1. 启动服务器时加载实例,Filter直接初始化,Servlet是第一次访问时初始化。
  2. Servlet从第一次到以后的多次访问,都是只调用doGet()或doPost()方法; Filter只调用方法doFilter()进行处理

实现Filter接口以后,会有三个方法:

  • public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)     //执行过滤操作
  • public void init(FilterConfig fConfig)                                                                                     //通过FilterConfig 获取Config 配置对象者其它域对象
  • public void destroy()                                                                                                      //销毁时执行

简单用法:

首先新建类 MyFilter 实现  Filter接口

//首先实现  Filter
public class MyFilter implements Filter { public MyFilters() {
// TODO Auto-generated constructor stub
} /**
* 销毁时执行
*/
public void destroy() {
// TODO Auto-generated method stub
} /**
* 在Serveice方法钱执行过滤操作
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(request, response);
} /**
* 获取配置信息
*/
public void init(FilterConfig fConfig) throws ServletException { }

然后在Web.xml配置文件中添加配置信息

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>com.wwj.Filter</display-name> <filter>
<filter-name>MyFilter</filter-name>
<!-- 类的限定名 -->
<filter-class>com.wwj.Filte.MyFilter</filter-class>
<init-param>
<param-name>ip</param-name>
<param-value>127.0.0.1</param-value>
</init-param>
</filter>
<filter-mapping>
<!-- 名称和上面的名称保持一致 -->
<filter-name>MyFilter</filter-name>
<!-- 过滤的URL地址,此种写法是过滤所有 -->
<url-pattern>/*</url-pattern>
<!-- 设置需要过滤的请求,默认是Request -->
<dispatcher>REQUEST</dispatcher>
</filter-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

这样一个简单的过滤器就配置好了!

下面演示一个过滤IP的小Demo:

首先在Web.xml配置需要过滤的IP地址

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>com.wwj.Filter</display-name> <filter>
<filter-name>MyFilter</filter-name>
<!-- 类的限定名 -->
<filter-class>com.wwj.Filte.MyFilter</filter-class>
<init-param>
<!-- 参数名,相当于Key -->
<param-name>ip</param-name>
<!-- 参数值,相当于Value -->
<param-value>127.0.0.1</param-value>
</init-param>
</filter>
<filter-mapping>
<!-- 名称和上面的名称保持一致 -->
<filter-name>MyFilter</filter-name>
<!-- 过滤的URL地址,此种写法是过滤所有 -->
<url-pattern>/*</url-pattern>
<!-- 设置需要过滤的请求,默认是Request -->
<dispatcher>REQUEST</dispatcher>
</filter-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

然后在程序里读取配置的需要过滤的IP地址,然后在doFilter进行过滤:

(服务器和客户端为一台机器时,地址不能写http:localhost:8008/xxx ,应该写  http:127.0.0.1:8008/xxx 这种方式)

//首先实现  Filter
public class MyFilters implements Filter { //定义变量
private FilterConfig config; public MyFilters() {
// TODO Auto-generated constructor stub
} /**
* 销毁时执行
*/
public void destroy() {
// TODO Auto-generated method stub
} /**
* 在Serveice方法钱执行过滤操作
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
//读取we.xml配置的IP地址
String FilterIp = this.config.getInitParameter("ip");
//获取当前客户端的地址
String ip = request.getRemoteAddr();
System.out.println(ip);
if (ip.equals(FilterIp)) {
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.write("<b>该IP被设置禁止访问!!!</b>");
} else {
System.out.println("IP不在过滤的名单之中");
chain.doFilter(request, response);
}
} /**
* 获取配置信息
*/
public void init(FilterConfig fConfig) throws ServletException {
this.config = fConfig; //赋值
}
}

JavaWeb学习篇--Filter过滤器的更多相关文章

  1. javaWeb学习之 Filter过滤器----https://www.cnblogs.com/xdp-gacl/p/3948353.html

    https://www.cnblogs.com/xdp-gacl/p/3948353.html

  2. angularjs学习第三天笔记(过滤器第二篇---filter过滤器及其自定义过滤器)

    您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...

  3. JavaWeb学习篇之----自定义标签&&JSTL标签库详解

    今天来看一下自定义标签的内容,自定义标签是JavaWeb的一部分非常重要的核心功能,我们之前就说过,JSP规范说的很清楚,就是Jsp页面中禁止编写一行Java代码,就是最好不要有Java脚本片段,下面 ...

  4. Servlet的学习之Filter过滤器技术(1)

    本篇将讲诉Servlet中一项非常重要的技术,Filter过滤器技术.通过过滤器,可以对来自客户端的请求进行拦截,进行预处理或者对最终响应给客户端的数据进行处理后再输出. 要想使用Filter过滤器, ...

  5. [转]Servlet的学习之Filter过滤器技术

    本篇将讲诉Servlet中一项非常重要的技术,Filter过滤器技术.通过过滤器,可以对来自客户端的请求进行拦截,进行预处理或者对最终响应给客户端的数据进行处理后再输出. 要想使用Filter过滤器, ...

  6. JavaWeb学习篇之----Servlet过滤器Filter和监听器

    首先来看一下Servlet的过滤器内容: 一.Servlet过滤器的概念: ************************************************************** ...

  7. Javaweb学习笔记9—过滤器

      今天来讲javaweb的第9阶段学习.   过滤器,我在本次的思维导图中将过滤器和监听器放在一起总结了,监听器比较简单就不单独写了.   老规矩,首先先用一张思维导图来展现今天的博客内容.     ...

  8. JavaWeb学习篇之----web应用的虚拟目录映射和主机搭建(Tomcat)

    从今天开始来学习JavaWeb的相关知识,之前弄过一段时间JavaWeb的,就是在做毕业设计的时候搞过,但是那时候完全是为了任务去学习,所以效果不好,好多东西都没有深入的研究过,所以接下来的一段时间我 ...

  9. JavaWeb学习笔记--filter开发

    介绍自定义的Filter类必须实现Filter接口,并且实现Filter接口定义的init() doFilter() destory()方法.其中init为初始化,destory为销毁 doFilte ...

随机推荐

  1. 黄聪:3分钟学会sessionStorage用法

    前言: 因最近移动端开发过程中遇到一个运营提出的所谓技术难点需求,对于原生APP来说轻而易举,毕竟自己的APP用户操作指哪打哪,但是H5该怎么做?H5就实现不了么?对于一个爱研究攻克这些前端棘手问题的 ...

  2. 黄聪:wordpress获取hook所有function

    list_hooked_functions('wp_footer'); function list_hooked_functions($tag=false) { global $wp_filter; ...

  3. python selenium right click on an href and choose Save link as... on Chrome.

    From:https://stackoverflow.com/questions/42781483/right-click-on-an-href-and-choose-save-link-as-in- ...

  4. 我的Netty笔记

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  5. 开启和关闭HBase的thrift进程

    开启 $HBASE_HOME/bin/hbase-daemon.sh start thrift [hadoop@bigdatamaster hbase]$ jps 3543 ThriftServer ...

  6. 【RPC】使用Hessian构建RPC的简单示例

    服务接口和实现 public interface HelloService { // 服务方法 String sayHello(String name); } public class HelloSe ...

  7. hadoop 完全分布式安装

    一个完全的hadoop分布式安装至少需要3个zookeeper,3个journalnode,3个datanode,2个namenode组成. 也就是说需要11个节点,但是我云主机有限,只有3个,所以把 ...

  8. 廖雪峰Java6IO编程-1IO基础-1IO简介

    1.IO简介 IO是指Input/Output,即输入和输出: Input指从外部读取数据到内存,例如从磁盘读取,从网络读取. * 为什么要把数据读到内存才能处理这些数据呢? * 因为代码是在内存中运 ...

  9. [UE4]条件融合动画: Blend Posed by int

    Aim Group=0:使用动画“Blend Pose 0” Aim Group=1:使用动画“Blend Pose 1”

  10. ACCESS常用数字类型的说明和取值范围

    下面是ACCESS常用数字类型的说明和取值范围列表明供参考 数字类型                 范围 Byte(字节)            介于 0 到 255 之间的整型数. Integer ...