Listener监听器
监听器
1) ServletContextListener - 监听ServletContext对象的创建和销毁的过程
2) HttpSessionListener - 监听HttpSession对象的创建和销毁的过程
3) ServletRequestListener - 监听ServletRequest对象的创建和销毁的过程
4) ServletContextAttributeListener - 监听ServletContext的保存作用域的改动(add,remove,replace)
5) HttpSessionAttributeListener - 监听HttpSession的保存作用域的改动(add,remove,replace)
6) ServletRequestAttributeListener - 监听ServletRequest的保存作用域的改动(add,remove,replace)
7) HttpSessionBindingListener - 监听某个对象在Session域中的创建与移除
8) HttpSessionActivationListener - 监听某个对象在Session域中的序列化和反序列化
MyServletContextListener 类
//@WebListener
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Servlet上下文对象初始化动作被我监听到了.....");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("Servlet上下文对象销毁动作被我监听到了......");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--
<filter>
<filter-name>Demo01Filter</filter-name>
<filter-class>com.atguigu.filters.Demo01Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>Demo01Filter</filter-name>
<url-pattern>/demo01.do</url-pattern>
</filter-mapping>
-->
<listener>
<listener-class>com.atguigu.listener.MyServletContextListener</listener-class>
</listener>
</web-app>
Tomcat(Web容器)启动时
![](https://img-blog.csdnimg.cn/a45aae4769034495ba5a1ab1e25643a7.png)
Tomcat(Web容器)关闭时
![](https://img-blog.csdnimg.cn/ed2ae7c212114eb4aba30db8cb3a6bd9.png)