打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
生命周期监听器和属性监听器
ServletContext
生命周期监听:ServletContextListener,它有两个方法,一个出生时调用,一个死亡时调用。
contextInitialized(ServletContextEventservletContextEvent)    创建ServletContext时调用
contextDestroyed(ServletContextEvent servletContextEvent)   销毁ServletContext时调用
属性监听:ServletContextAttributeListener它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个在移除属性时调用。
attributeAdded(ServletContextAttributeEventscab)           添加属性时调用
attributeReplaced(ServletContextAttributeEventscab)       替换属性时调用
attributeRemoved(ServletContextAttributeEventscab)      移除属性时调用
HttpSession
生命周期监听:HttpSessionListener,它有两个方法,一个出生时调用,一个死亡时调用。
sessionCreated(HttpSessionEventhttpSessionEvent) 创建HttpSession时调用
sessionDestroyed(HttpSessionEventhttpSessionEvent)    销毁HttpSession时调用
属性监听:HttpSessionAttributeListener它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个在移除属性时调用。
attributeAdded(HttpSessionBindingEvent se)              添加属性时调用
attributeReplaced(HttpSessionBindingEvent se)  替换属性时调用
attributeRemovedHttpSessionBindingEvent se)   移除属性时调用
ServletRequest
生命周期监听:ServletRequestListener,它有两个方法,一个出生时调用,一个死亡时调用。
requestInitialized(ServletRequestEventservletRequestEvent) 创建ServletRequest时调用
requestDestroyed(ServletRequestEventservletRequestEvent) 销毁ServletRequest时调用
属性监听:ServletRequestAttributeListener它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个在移除属性时调用。
attributeAdded(ServletRequestAttributeEvent srae)           添加属性时调用
attributeReplaced(ServletRequestAttributeEvent srae)      替换属性时调用
attributeRemoved(ServletRequestAttributeEvent srae)      移除属性时调用
开始做个“生命周期监听:ServletContextListener”的小例子
创建一个类实现ServletContextListener接口
package cn.loubth.web;
importjavax.servlet.ServletContextEvent;
importjavax.servlet.ServletContextListener;
publicclass TestListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext今天诞生!");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext结束了他的一生!");
}
}
接下来要配置web.xml配置文件
在web-app标签下配置
<listener>
<listener-class>cn.loubth.web.TestListener</listener-class>
</listener>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Web-第十七天 监听器Listener【悟空教程】
关于JSP.Servlet启动次序的说明
Java三大器之监听器(Listener)的工作原理和代码演示
转:SpringMVC理解之一:应用上下文webApplicationContext
ServletContextListener使用详解
Servlet 中的 Listener 的应用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服