大型项目中侦听器类的用途是什么
我对监听器类的作用感到困惑。例如,在这个项目中,有一个监听器类被这样引用:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
侦听器类会收到有关选定事件的通知,例如启动应用程序或创建新会话。
听众课程:
来源
Listener Classes get notified on selected events, such as starting up the application or creating a new Session.
Listener Classes :
Source
我建议查看 Servlet 规范中有关“应用程序生命周期事件”的章节。
根据您使用的版本,以下是相应的章节和文档链接:
监听器的使用接收 Web 应用程序的事件通知,包括 ServletContext、HttpSession 和 ServletRequest 对象中的状态更改。通过实现预定义的侦听器接口 (
javax.servlet.ServletContextListener< /code>
,
javax.servlet.http.HttpSessionListener
,javax.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
, andServletRequest
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.是的,他们正在监听一些待办事项,例如,如果它的 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
更一般地说,侦听器是观察者模式中的观察者/订阅者端。服务器/框架端为您提供了一种通知某些事件的方法,因此给您一个执行操作的机会。
而且它不一定是“一个大项目”。即使在较小的听众中,听众也很方便:)。
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 :).