大型项目中侦听器类的用途是什么

发布于 2024-12-06 04:28:36 字数 238 浏览 0 评论 0原文

我对监听器类的作用感到困惑。例如,在这个项目中,有一个监听器类被这样引用:

<listener>
    <listener-class>com.sun.javaee.blueprints.petstore.model.CatalogFacade</listener-class> 
</listener>

是不是顾名思义,只是监听要执行的操作?

I'm confused about what listener classes do. For example, in this project there is a listener class referenced as so:

<listener>
    <listener-class>com.sun.javaee.blueprints.petstore.model.CatalogFacade</listener-class> 
</listener>

Is it as the name implies, just listening for actions to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

又怨 2024-12-13 04:28:36

侦听器类会收到有关选定事件的通知,例如启动应用程序或创建新会话。

听众课程:

这些是简单的 Java 类,它们实现以下两个之一
接口:

  • javax.servlet.ServletContextListener
  • javax.servlet.http.HttpSessionListener

如果您希望您的类监听应用程序的启动和关闭
然后事件实现 ServletContextListener 接口。如果你想
然后你的类监听会话创建和失效事件
实现HttpSessionListener接口。

来源

Listener Classes get notified on selected events, such as starting up the application or creating a new Session.

Listener Classes :

These are simple Java classes which implement one of the two following
interfaces :

  • javax.servlet.ServletContextListener
  • javax.servlet.http.HttpSessionListener

If you want your class to listen for application startup and shutdown
events then implement ServletContextListener interface. If you want
your class to listen for session creation and invalidation events then
implement HttpSessionListener interface.

Source

靑春怀旧 2024-12-13 04:28:36

我建议查看 Servlet 规范中有关“应用程序生命周期事件”的章节。

根据您使用的版本,以下是相应的章节和文档链接:

监听器的使用接收 Web 应用程序的事件通知,包括 ServletContext、HttpSession 和 ServletRequest 对象中的状态更改。通过实现预定义的侦听器接口 (javax.servlet.ServletContextListener< /code>, javax.servlet.http.HttpSessionListenerjavax.servlet.ServletRequestListener 等),servlet 容器将通知您应用程序中发生的某些事件。它们有很多潜在用途,例如执行一次性应用程序设置和关闭任务、拦截请求以执行日志记录、跟踪 HTTP 会话使用等。

I'd suggest reviewing the chapter on "Application Lifecycle Events" from the Servlet specification.

Depending on which version you're using, here are the corresponding chapters and links to the docs:

Listeners are used to be notified of events to web applications, including state changes in the ServletContext, HttpSession, and ServletRequest objects. By implementing predefined listener interfaces (javax.servlet.ServletContextListener, javax.servlet.http.HttpSessionListener, javax.servlet.ServletRequestListener, etc.), the servlet container will notify you of certain events that are happening in your application. They have a lot of potential uses, such as performing one-time application setup and shutdown tasks, intercepting requests to perform logging, tracking HTTP session use, etc.

不即不离 2024-12-13 04:28:36

是的,他们正在监听一些待办事项,例如,如果它的 contextloaderlistener 那么它将监听上下文加载事件,并且我们可以在此类事件中做很多事情,因此这些是为此而制作的

Yes exactly they are listening for some action todo, for example if its contextloaderlistener then it will listen to context loading event and there are many stuff that we can do at such event so these are made for that

情感失落者 2024-12-13 04:28:36

更一般地说,侦听器是观察者模式中的观察者/订阅者端。服务器/框架端为您提供了一种通知某些事件的方法,因此给您一个执行操作的机会。

而且它不一定是“一个大项目”。即使在较小的听众中,听众也很方便:)。

More generally, a listener is the observer/subscriber side in the observer pattern. The server/framework side provides you with a means to be notified of some event and therefore give you a chance to do your actions.

And it not necessarily must be "a large project". Listeners come handy even in the smaller ones :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文