更新时间:2023-12-04 10:34:28
A
过滤器是一个对象,它对对资源(servlet或静态内容)的请求或对资源的响应(或两者)执行过滤任务.
过滤器以
doFilter
方法执行过滤.每个Filter
都可以访问一个FilterConfig
对象,从中可以获取其初始化参数,并可以引用一个ServletContext
对象,例如,它可以用于加载筛选任务所需的资源.过滤器是在Web应用程序的部署描述符中配置的.
为此设计确定的示例为:
- 身份验证过滤器
- 记录和审核过滤器
- 图像转换滤镜
- 数据压缩过滤器
- 加密过滤器
- 标记过滤器
- 触发资源访问事件的过滤器
- XSL/T过滤器
- Mime型链式过滤器
ServletContextListener
会拦截Webapp的启动和关闭,并允许您在启动和/或关闭时执行一些代码.另请参见其 javadoc :
用于接收有关
ServletContext
生命周期更改的通知事件的接口.为了接收这些通知事件,必须在Web应用程序的部署描述符中声明实现类,并用
WebListener
进行注释,或者通过在ServletContext
上定义的addListener
方法之一进行注册. /p>此接口的实现以其声明的顺序在其
contextInitialized(javax.servlet.ServletContextEvent)
方法中调用,并以相反的顺序在其contextDestroyed(javax.servlet.ServletContextEvent)
方法中调用.
现在应该清楚何时使用一个或另一个.如果您想拦截/修改HTTP请求/响应,而想通过HTTP请求来拦截特定的URL模式,请使用Filter
.如果要拦截Webapp的启动和/或关闭操作,请使用ServletContextListener
.
请知道在哪里可以找到javadocs以及如何解释它们.它们包含对此类琐碎问题的所有答案.
What are the differences between using a Servlet Filter versus a Servlet Context Listener?
When would you use one or the other?
A Filter
intercepts on HTTP requests matching its URL pattern and allows you to modify them. See also its javadoc:
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. EveryFilter
has access to aFilterConfig
object from which it can obtain its initialization parameters, and a reference to theServletContext
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
A ServletContextListener
intercepts on webapp's startup and shutdown and allows you to execute some code on startup and/or shutdown. See also its javadoc:
Interface for receiving notification events about
ServletContext
lifecycle changes.In order to receive these notification events, the implementation class must be either declared in the deployment descriptor of the web application, annotated with
WebListener
, or registered via one of theaddListener
methods defined onServletContext
.Implementations of this interface are invoked at their
contextInitialized(javax.servlet.ServletContextEvent)
method in the order in which they have been declared, and at theircontextDestroyed(javax.servlet.ServletContextEvent)
method in reverse order.
When to use the one or the other should now be obvious. Use a Filter
if you want to intercept on HTTP requests maching a specific URL pattern because you want to check/modify the HTTP request/response. Use a ServletContextListener
if you want to intercept on webapp's startup and/or shutdown.
Please know where to find the javadocs and how to interpret them. They contain all answers to this kind of trivial questions.