javax.servlet.Filter

javax.servlet.Filter 接口

http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html

接口原型

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();
}

Filter接口文档

英文文档

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content),
or on the response from a resource, or both. 

Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object
from which it can obtain its initialization parameters, and a reference to the ServletContext which it can use,
for example, to load resources needed for filtering tasks. 

Filters are configured in the deployment descriptor of a web application. 

Examples that have been identified for this design are:
    Authentication Filters
    Logging and Auditing Filters
    Image conversion Filters
    Data compression Filters
    Encryption Filters
    Tokenizing Filters
    Filters that trigger resource access events
    XSL/T filters
    Mime-type chain Filter 

Since:
    Servlet 2.3

中文翻译

Filter 是一个在 资源请求(servlet或静态内容) 或 内容响应 中执行过滤任务的对象。

Filter 在 doFilter 方法内执行过滤。每一个 Filter 可以访问一个 FilterConfig 对象,
通过该对象,filter 可以获取到初始化参数和 ServletContext 引用,该引用可以用于加载过滤任务的所需的资源。

Filter 被配置在一个 Web 部署描述(文件)中。

已经确定可用于的设计例子如下:
    身份认证 Filters
    日志和审计 Filters
    图像转换 Filters
    数据压缩 Filters
    加密 Filters
    令牌化 Filters
    触发资源访问事件的 Filters
    XSL/T filters
    Mime-type chain (mime类型链) Filter 

Since:
    Servlet 2.3

init 方法文档

英文文档

public void init(FilterConfig filterConfig) throws ServletException;

    Called by the web container to indicate to a filter that it is being placed into service. 

    The servlet container calls the init method exactly once after instantiating the filter.
    The init method must complete successfully before the filter is asked to do any filtering work. 

    The web container cannot place the filter into service if the init method either
        Throws a ServletException
        Does not return within a time period defined by the web container 

    Parameters:
        filterConfig 

    Throws:
        ServletException

中文翻译

    此方法被容器调用,用于预示此 filter 正在被放入 service。

    一旦 filter 初始化完,Servlet 容器会正确地调用此 init 方法。在被要求执行过滤任务前,次 init 方法必须执行完成。

    出现以下情况时,Web 容器不会把此 filter 对象放入 service:
        抛出 ServletException 时
        在 Web 容器规定的一段时间周期内没有返回时

    Parameters:
        filterConfig 

    Throws:
        ServletException

doFilter 方法注释

英文文档

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

    The doFilter method of the Filter is called by the container
    each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
    The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

    A typical implementation of this method would follow the following pattern: 

        1. Examine the request
        2. Optionally wrap the request object with a custom implementation to filter content or headers for input filtering
        3. Optionally wrap the response object with a custom implementation to filter content or headers for output filtering 

        4. Either invoke the next entity in the chain using the FilterChain object (chain.doFilter()),
            or not pass on the request/response pair to the next entity in the filter chain to block the request processing
        5.Directly set headers on the response after invocation of the next entity in the filter chain. 

    Parameters:
        request
        response
        chain
    Throws:
        IOException
        ServletException

中文翻译

    Filter 的 doFilter 方法由容器调用,每一次客户端请求资源时,都会有一对 request/response 被传递到 filter chain。
    FilterChain 对象被传入此方法,允许 Fitler 把 request/response 传递给FilterChain里的下一个 filter。请求的资源会在 chain 执行完之后才被访问到。

    此方法的标准实现将遵循以下模式:
        1. 检查请求
        2. 选择性地,为 input filtering 使用使用自定义的 filter content 或 headers 实现对请求进行转化。
        3. 选择性地,为 output filtering 使用使用自定义的 filter content 或 headers 实现对响应进行转化。
        4. 使用 FilterChain 对象的 chain.doFilter() 方法调用 filter chain 里的下一个filter, 或不再把一对 request/response 传给filter chain 里的下一个filter,阻止该请求处理。
        5. 在调用 filter chain 里的下一个filter 之后,直接设置 response 的 headers。

    Parameters:
        request
        response
        chain
    Throws:
        IOException
        ServletException

destroy 方法注释

英文文档

public void destroy();

    Called by the web container to indicate to a filter that it is being taken out of service. 

    This method is only called once all threads within the filter‘s doFilter method have exited or after a timeout period has passed.
    After the web container calls this method, it will not call the doFilter method again on this instance of the filter. 

    This method gives the filter an opportunity to clean up any resources that are being held (for example, memory, file handles, threads)
    and make sure that any persistent state is synchronized with the filter‘s current state in memory.

中文翻译

    此方法被容器调用,用于预示此 filter 正在被移出 service。

    此方法仅会在所有在 doFilter 里的线程已退出或者一段时间周期之后被调用。
    当 web 容器调用此方法之后,容器将不会再调用此 filter 实例的 doFilter 方法。

    此方法给了 filter 一个机会去释放被自己占用的所有资源(例如:内存,文件句柄,线程等),
    和去确保所有持久化的状态与当前内存中的状态同步。

总结

  1. Filter 用于请求和响应流程中进行滤。
  2. init 和 destroy 是 Filter 的生命周期方法,由 Servlet 容器调用。
    • 一旦 filter 初始化完成,容器会调用 Filter 的 init 方法,预示 filter 正在被放入 service。
    • filter 被移出 service 时,容器会调用 Filter 的 destroy 方法。
  3. Filter 及 FilterChain 会在请求的目标资源被访问之前调用。
  4. doFilter 方法执行过滤逻辑。执行完过滤逻辑后,可以调用 chain.doFilter() 方法继续执行下一个 filter,也可不再调用,从而阻止请求处理。
时间: 2024-08-28 00:29:11

javax.servlet.Filter的相关文章

can not be cast to javax.servlet.Filter等问题的解决方法

问题1:can not be cast to javax.servlet.Filter 一位从测试转开发的同事首次更新代码,并编译在本地启动web项目时,报如下错误: Exception starting filter encodingFilterjava.lang.ClassCastException:com.gaochao.platform.web.context.filter.ContextFilter2 can not be cast to javax.servlet.Filter 根据

java.lang.IllegalStateException: class utils.filter.ContentFilter is not a javax.servlet.Filter

1.错误描述 2016-01-12 11:27:01.787:WARN:oejuc.AbstractLifeCycle:FAILED ContentFilter: java.lang.IllegalStateException: class utils.filter.ContentFilter is not a javax.servlet.Filter java.lang.IllegalStateException: class utils.filter.ContentFilter is not

cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突

使用maven开发web应用程序, 启动的时候报错: jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class 然后输出错误: 严重: Exception starting filter encodingFilter java.lang.ClassCastException: org.springframework.web.filter.CharacterEn

在maven项目中使用apache cxf中遇到异常 java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter

使用maven虽然很方便,写一个dependency的标签就可以直接从仓库下载对应的jar包,还能处理该jar包的继承依赖关系.但是同时需要你对jar包更加了解,如果依赖关系很复杂,那么很可能会产生jar包冲突,从而使项目报一些莫名其妙的异常. 在用apache cxf的过程中就出现了这样的问题. 1,在项目的pom.xml中加入apache cxf的依赖配置: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs

maven struts2工程StrutsPrepareAndExecuteFilter cannot be cast to javax.servlet.Filter

maven搭建struts2工程时报错 严重: Exception starting filter struts2java.lang.ClassCastException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter cannot be cast to javax.servlet.Filter 工程是复制的,所以从pom.xml导入时一些jar包引用清理的不干净,最后找到原因是与servlet-api

Java-maven异常-cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突

使用maven开发web应用程序, 启动的时候报错: jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class 然后输出错误: 严重: Exception starting filter encodingFilter java.lang.ClassCastException: org.springframework.web.filter.CharacterEn

理解Servlet过滤器(javax.servlet.Filter)

佟强  2009年12月14日 过滤器(Filter)的概念 过滤器位于客户端和web应用程序之间,用于检查和修改两者之间流过的请求和响应.在请求到达Servlet/JSP之前,过滤器截获请求.在响应送给客户端之前,过滤器截获响应.多个过滤器形成一个过滤器链,过滤器链中不同过滤器的先后顺序由部署文件web.xml中过滤器映射<filter-mapping>的顺序决定.最先截获客户端请求的过滤器将最后截获Servlet/JSP的响应信息.过滤器的链式结构 可以为一个Web应用组件部署多个过滤器,

找不到javax.servlet.Filter的类文件

在这里我是用IDEA来开发的,Tomcat用的maven插件 原因:没有相应在jar包 解决:导入相应在jar的依赖,在pom文件中添加 <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> 原文地址:https://www.cnblogs.co

Servlet Filter 中init和destroy问题

测试源码如下: 1 package com.FilterTest.Filter; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.Servlet