注意:
所有监听器中的方法都不需要java程序员调用,都是服务器自己调用的,
只在这个特殊时刻编写自己的代码即可
package indi.yuluo.javaweb.listener;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;
@WebListener
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContext对象创建了。");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContext对象被销毁了。");
}
}
package indi.yuluo.javaweb.listener;
import jakarta.servlet.ServletRequestEvent;
import jakarta.servlet.ServletRequestListener;
import jakarta.servlet.annotation.WebListener;
@WebListener
public class MyServletRequestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("request对象销毁了");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("request对象初始化了");
}
}