gpt4 book ai didi

java - 从 ServletContext 中移除监听器

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:42 27 4
gpt4 key购买 nike

我想知道如何从 ServletContext ( http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html ) 中删除 EventListener。后者提供了一个 addListener 方法,但没有删除监听器的方法。听众是否可能被弱引用持有并且可以通过丢失所有强引用来注销?或者如果我的应用程序未部署会怎样?监听器是否仍然活跃?

最佳答案

这不是问题的答案,我只是想说明几点。

The later offers a addListener method but nothing to remove a listener.

因为您需要先决定是否要有监听器。看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.

addListener()为您提供注册多种类型的监听器的规定,而不仅仅是 ServletContextListener。如果您选择不在 web.xml 中预先声明监听器,您可以使用它。

The given listener must be an instance of one or more of the following interfaces:

  1. ServletContextAttributeListener
  2. ServletRequestListener
  3. ServletRequestAttributeListener
  4. HttpSessionListener
  5. HttpSessionAttributeListener

what happens if my application is undeployed

所有监听器对象将与应用程序一起销毁。监听器在部署 Web 应用程序时在 Web 容器中实例化和注册。

监听器类使用监听器元素在 Web 应用程序部署描述符中声明。它们按调用顺序按类名列出。在 Web 应用程序执行期间,监听器按其注册顺序被调用。在应用程序关闭时,监听器会以与其声明相反的顺序收到通知,在通知上下文监听器之前先通知 session 监听器。在通知上下文监听器应用程序关闭之前,必须先通知 session 监听器 session 失效。

关于java - 从 ServletContext 中移除监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17673141/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com