Java Servlet (1) —— Filter过滤请求与响应


版本: Java EE 6

参考来源:

Oracle:The Java EE 6 Tutorial: Filtering Requests and Responses

CSDN:Java中Filter、Servlet、Listener的学习

CSDN:filter与servlet的比较

正文

oracle javaee 6的官方文档中短短的一段话,分别从定义、内容、应用、实现这四个方面对Filter这个东西做了详细的说明

定义

A filter is an object that can transform the header and content (or both) of a request or response. Filters differ from web components in that filters usually do not themselves create a response. Instead, a filter provides functionality that can be “attached” to any kind of web resource. Consequently, a filter should not have any dependencies on a web resource for which it is acting as a filter; this way, it can be composed with more than one type of web resource.

以上定义有几层意思:

  1. Filter是一个对象

    (A filter is an object)

  2. Filter对象的功能是可以变换请求或相应的头和内容

    (can transform the header and content (or both) of a request or response)

  3. Filter与web components不同,不自己创建相应

    (Filters differ from web components in that filters usually do not themselves create a response)

    Web Components是什么?(Wiki:Web Components

    Wiki上的定义比较抽象,但是它也给出了Web Components所表现的几个具象形式:

    • 自定义元素(Custom Elements)

    • 隐藏DOM(Shadow DOM)

    • HTML引入(HTML Imports)

    • HTML模板(HTML Templates)

    总而言之,Web Components可以认为是一些资源(resource)的组件。

    为什么我将它看成资源的组件?下面这点可以看出(Instead...web resource)

  4. Filter可以“附在”(attached)任何web资源上

    (Instead, a filter provides functionality that can be “attached” to any kind of web resource)

  5. Filter不应依赖与它“依附”的web资源

    (Consequently, a filter should not have any dependencies on a web resource for which it is acting as a filter)

    这点是与上第4点对应。第4点为正说:应该怎样;这里为反说:不应怎样。

  6. Filter可以与多个web资源组合在一起使用

    (this way, it can be composed with more than one type of web resource)

    正因为有4、5两特点,所以Filter具有这种能力。

何种能力呢?

功能

The main tasks that a filter can perform are as follows:

  • Query the request and act accordingly.
  • Block the request-and-response pair from passing any further.
  • Modify the request headers and data. You do this by providing a customized version of the request.
  • Modify the response headers and data. You do this by providing a customized version of the response.
  • Interact with external resources.

Filter的主要功能包括:

  • 查询请求然后做相应动作

    (Query the request and act accordingly)

    这里“查询”(Query)主要体现在filter-mapping中的url-pattern。

  • 拦截请求与响应对(在向下传递时)

    (Block the request-and-response pair from passing any further)

    注意这里是请求与响应对,这个“对”(pair)十分重要。

  • 修改请求的头与数据

    (Modify the request headers and data. You do this by providing a customized version of the request)

  • 修改响应的头与数据

    (Modify the response headers and data. You do this by providing a customized version of the response)

  • 与外部资源交互

    (Interact with external resources)

    以上这点比较抽象。与什么样的外部资源?如何交互?

暂且不回答这个问题,看Filter的应用场景。

应用

  • 验证(Authentication)

    例如SSO等验证实现都有AuthenticationFilter。

  • 日志(Logging)

    为了实现任何Filter的应用,都可以加入日志之类的功能。

  • 图像转换(Image Conversion)

    主要常见于图像格式的转换,根据不同客户端可能支持显示的格式不同,处理图片响应。

  • 数据压缩(Data Compression)

    对于较大的请求与响应体,可以设置数据压缩GZipFilter。

  • 加密(Encryption)

    对于SSL或者自行实现的安全措施,会对请求与响应进行加密。

  • 标记流(Tokenizing Streams)

    这个主要见于搜索应用中,比如Elastic会有TokenFilter。

  • XML变换(XML transformations)

    一个典型应用可能是使用xslt转换xml的内容。

如此看来,功能中的最后一点中提到的“与外部资源的交互”就很好理解了,以上的这些验证、加密、压缩、变换等功能都需要外部资源的支持。

实现

最后实现也只是两句话,但是足以将Filter的内涵说清楚。

You can configure a web resource to be filtered by a chain of zero, one, or more filters in a specific order.

这里提到了几个关键点:

  • 目标——配置web资源(web resource)
  • 方式——链式(chain)
  • 数量——0、1或多(zero, one, or more filters)
  • 顺序——特定的顺序(in a specific order)

This chain is specified when the web application containing the component is deployed and is instantiated when a web container loads the component.

补充说明链式是如何工作的:

  • 编译时(静态)——在编译部署的时候,这个链就已经定义好了。
  • 运行时(动态)——在加载组件的时候,这个链被实例化。

至于详细实现方式,另开文章做具体说明。

原文

Filtering Requests and Responses

A filter is an object that can transform the header and content (or both) of a request or response. Filters differ from web components in that filters usually do not themselves create a response. Instead, a filter provides functionality that can be “attached” to any kind of web resource. Consequently, a filter should not have any dependencies on a web resource for which it is acting as a filter; this way, it can be composed with more than one type of web resource.

The main tasks that a filter can perform are as follows:

  • Query the request and act accordingly.
  • Block the request-and-response pair from passing any further.
  • Modify the request headers and data. You do this by providing a customized version of the request.
  • Modify the response headers and data. You do this by providing a customized version of the response.
  • Interact with external resources.

Applications of filters include authentication, logging, image conversion, data compression, encryption, tokenizing streams, XML transformations, and so on.

You can configure a web resource to be filtered by a chain of zero, one, or more filters in a specific order. This chain is specified when the web application containing the component is deployed and is instantiated when a web container loads the component.

*扩展

问题

Filter有以上的职责,那么Interceptor的主要作用是什么呢?

结束

Java Servlet (1) —— Filter过滤请求与响应的更多相关文章

  1. Java web入门之Http请求和响应

    三层架构 web层:JSP + Servlet.Struts 2.SpringMVC service层:Spring dao层:JDBC.DBUtils.Hibernate.MyBatis form表 ...

  2. Java Servlet(九):转发请求与重定向请求区别

    转发: <% pageContext.setAttribute("pageContextAttr", "pageContextAttribute"); r ...

  3. 【Spring】使用Filter过滤请求

    原文:http://liujiajia.me/#/blog/details/spring-filter-request-with-filter public class CustomizedFilte ...

  4. 简单的Servlet结合Jsp实现请求和响应以及对doGet和doPost的浅析

    1.新建jsp,创建表单 <body> <form action="/MyfirstWeb/login"> username:<input type= ...

  5. 关于servlet的filter

    Servlet过滤器 2009-12-08 23:12:44|  分类: Java|举报|字号 订阅     一.什么是Servlet过滤器 过滤器是在数据交互之间过滤数据的中间组件,独立于任何平台或 ...

  6. Servlet、Filter、Listener、Interceptor基础

    第一:Servlet Servlet是个接口,全限定名是javax.servlet.Servlet,在javax.servlet包中,在servlet-api.jar(在tomcat自带的lib文件夹 ...

  7. Java Servlet API中文说明文档

    Java Servlet API中文说明文档 目 录 1.... Servet资料 1.1      绪言 1.2      谁需要读这份文档 1.3      Java Servlet API的组成 ...

  8. java Servlet中的过滤器Filter

    web.xml中元素执行的顺序listener->filter->struts拦截器->servlet. 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...

  9. Java基础ArrayList、Servlet与Filter

    一.技术分享 迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为"轻量级"对象,因 ...

随机推荐

  1. Eclipse设置项目默认编码和换行符类型

    为了实现不同操作系统间的Eclipse项目移植的一致性. 一.建议设置如下: 1. 默认的项目编码统一用UTF-8 2. 默认的换行符用UNIX类型 二.具体的配置点见下图:

  2. Android Studio开发-高效插件强烈推荐

    Android Studio开发-高效插件强烈推荐 现在Android的开发者基本上都使用Android Studio进行开发(如果你还在使用eclipse那也行,毕竟你乐意怎么样都行).使用好And ...

  3. Python 学习参考书目推荐

    Python 学习,参考书目推荐 前言 好的技术书籍可以帮助我们快速地成长,大部分人或多或少地受益于经典的技术书籍.在「Python开发者」微信公号后台,我们经常能收到让帮忙推荐书籍的消息.这类的问题 ...

  4. HBase随机读写

    HDFS不太适合做大量的随机读应用,但HBASE却特别适合随机的读写 个人理解: 1.数据库一般都会有一层缓存,任何对数据的更改实际上是先更改内存中的数据.然后有异步的守护进程负责将脏页按照一定策略刷 ...

  5. vim快速指南

    vi编辑器有3种模式:命令模式.输入模式.末行模式.掌握这三种模式十分重要: 命令模式:vi启动后默认进入的是命令模式,任何模式下,按[Esc]键都可以返回命令模式.输入模式:可输入字符,在底部显示“ ...

  6. 对于android浏览器的一些看法

    首先我先声明我不是一个浏览器开发者,只是近段时间看了一些关于浏览器的东西,才有一些看法. 在几年前开发手机的web 页面,都经常因为JS插件不兼容android WebView内核,导致开发浪费大量时 ...

  7. 【转】10个非常有用的网页设计工具 | Goodfav Magazine

    10+ very useful Web Designer Tools Totally free legal computer eBooks download, available in various ...

  8. R语言之——字符串处理函数

    nchar 取字符数量的函数 length与nchar不同,length是取向量的长度 # nchar表示字符串中的字符的个数 nchar("abcd") [1] 4 # leng ...

  9. 最NB的发现 LINUX 下玩teamviewer 命令行设置密码

    cd /opt/teamviewer/tv_bin/ [root@666 tv_bin]# ls desktop script teamviewerd TVGuiSlave.32 xdg-utils ...

  10. Introducing Project Kinect for Azure

    https://www.linkedin.com/pulse/introducing-project-kinect-azure-alex-kipman/ Hello everyone! Microso ...