且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Servlet过滤器和Servlet上下文侦听器之间有什么区别?

更新时间:2023-12-04 10:34:28

A 截获与其URL模式匹配的HTTP请求,并允许您对其进行修改.另请参见其 javadoc :

过滤器是一个对象,它对对资源(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. 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

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 the addListener methods defined on ServletContext.

Implementations of this interface are invoked at their contextInitialized(javax.servlet.ServletContextEvent) method in the order in which they have been declared, and at their contextDestroyed(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.