实现监听ServletContext HttpSession ServletRequest这三个域对象的生命周期
创建监听器MyListenerpublic class MyListener implements
ServletContextListener, HttpSessionListener,ServletRequestListener {
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContext对象被创建了");
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContext对象被销毁了");
}
public void requestInitialized(ServletRequestEvent arg0) {
System.out.println("ServletRequest对象被创建了");
}
public void requestDestroyed(ServletRequestEvent arg0) {
System.out.println("ServletRequest对象被销毁了");
}
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("HttpSession对象被创建了");
}
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("HttpSession对象被销毁了");
}
}
web.xml中配置监听器
启动项目,查看ServletContext对象创建信息 关闭项目,查看ServletContext对象销毁信息 创建测试页面myjsp.jspcn.itcast.chapter08.listener.MyListener
测试HttpSessionListener,ServletRequestListener
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
this is MyJsp.jsp page
这是一个测试监听器的页面
设置监听器超时时间
再次启动项目,查看结果2
访问测试页面http://localhost:8080/chapter08/myjsp.jsp
两分钟后或者浏览器关闭,Session对象都会被销毁,所以控制台输出以下内容



