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) 替换属性时调用
attributeRemoved
HttpSessionBindingEvent 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>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。