1、Listener 监听器它是 JavaWeb 的三大组件Servlet 程序、Filter 过滤器、Listener监听器之一。
2、Listener 它是 JavaEE 的规范,就是接口。
3、监听器的作用是,监听某种事物的变化。然后通过回调函数,反馈给客户(程序)去做一些相应的处理。
现在常用的监听器便是ServletContextListener 监听器,它可以监听ServletContext 对象的创建和销毁。
ServletContext 对象在 web 工程启动的时候创建,在 web 工程停止的时候销毁。
监听到创建和销毁之后都会分别调用 ServletContextListener 监听器的方法反馈。
如何使用 ServletContextListener 监听器监听 ServletContext 对象
使用步骤如下:
public class MyServletContextListener implements ServletContextListener,HttpSessionListener, HttpSessionAttributeListener {// Public constructor is required by servlet specpublic MyServletContextListener() {}public void contextInitialized(ServletContextEvent sce) {/* This method is called when the servlet context isinitialized(when the Web application is deployed). You can initialize servlet context related data here.*/System.out.println("监听器启动");}public void contextDestroyed(ServletContextEvent sce) {/* This method is invoked when the Servlet Context (the Web application) is undeployed or Application Server shuts down.*/System.out.println("监听器销毁");}
}
这个比Servlet简单很多
<listener><listener-class>com.stackery.listener.MyServletContextListener</listener-class>
</listener>
本文发布于:2024-02-02 15:28:41,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170685892044713.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |