Filter(过滤器)是一个可以对资源请求(资源:servlet或静态内容)或资源响应,或者两个过程都执行过滤任务的对象。
filter对象是在 doFilter方法中执行过滤任务的。每个Filter都可以访问FilterConfig对象,从中可以获取其初始化参数,例如ServletContext的引用,可以使用ServletContext来加载过滤任务所需的资源。
一些filter的例子,比如:
1) Authentication Filters
2) Logging and Auditing Filters
3) Image conversion Filters
4) Data compression Filters
5) Encryption Filters
6) Tokenizing Filters
7) Filters that trigger resource access events
8) XSL/T filters
9) Mime-type chain Filter
default void | destroy()
Called by the web container to indicate to a filter that it is being taken out of service.
|
void | doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
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.
|
default void | init(FilterConfig filterConfig)
Called by the web container to indicate to a filter that it is being placed into service.
|
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException
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. a) Either invoke the next entity in the chain using the FilterChain object (chain.doFilter()),
4. b) 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- The request to process -
response- The response associated with the request -
chain- Provides access to the next filter in the chain for this filter to pass the request and response to for further processing
Filter(过滤器)是一种能够对客户端请求资源或响应进行过滤的对象。它可以在请求到达目标资源前或响应返回给客户端前执行一系列操作,如身份验证、日志记录等。本文将详细介绍Filter的工作原理及其常见应用场景。

被折叠的 条评论
为什么被折叠?



