Listener
概念:Listener表示监听器,是JavaWeb三大组件之一
监听器可以监听就是在application,session,requsest三个对象创建,销毁或者往其中
添加修改删除属性时自动执行代码的功能组件
Listener分类:JavaWeb中提供了8个监听器
ServletContext监听
监听器分类 | 监听器名称 | 作用 |
---|---|---|
ServletContext监听 | ServletContextListener | 用于对ServletContext对象进行监听(创建,销毁) |
ServletContextAttributeListener | 用于对ServletContext对象中的属性的监听(增删改) | |
Session监听 | HttpSessionListener | 对Session对象整体状态的监听(创建,销毁) |
HttpSessionAttributeListener | 用于对t对象中的属性的监听(增删改) | |
HttpSessionBindingListener(无需在web.xml中配置) | 监听Session作用域中监听对象的绑定与解绑 | |
HttpSessionActivationListener | 对Session数据进行钝化和活化的监听 | |
Request监听 | ServletRequestListener | 对Resquest对象进行监听(创建、销毁) |
ServletRequestAttributeListener | 对Request对象中的数学监听(增删改查) |
@WebListener
public class ContexLoderListern implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//加载资源
System.out.println("ContexLoderListern");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
//释放资源
}
}
运行程序会被自动执行