在javaweb中三大域对象都有自己的监听器(Servletcontext,HttpSesson,ServletRequest)
-
生命周期监听器
ServletContextListener:Tomcat启动和关闭时调用下面两个方法 public void contextInitialized(ServletContextEvent evt):ServletContext对象被创建后调用; public void contextDestroyed(ServletContextEvent evt):ServletContext对象被销毁前调用; HttpSessionListener:开始会话和结束会话时调用下面两个方法 public void sessionCreated(HttpSessionEvent evt):HttpSession对象被创建后调用; public void sessionDestroyed(HttpSessionEvent evt):HttpSession对象被销毁前调用; ServletRequestListener:开始请求和结束请求时调用下面两个方法 public void requestInitiallized(ServletRequestEvent evt):ServletRequest对象被创建后调用; public void requestDestroyed(ServletRequestEvent evt):ServletRequest对象被销毁前调用。
注意:需要在自定义的Listener类中继承生命周期监视器,并在当前项目的WebRoot/WeB-INF/web.xml中配置监听器的实现类
下面对这三个监听器的事件对象功能进行介绍:
ServletContextEvent:ServletContext getServletContext();
HttpSeessionEvent:HttpSession getSession();
ServletRequestEvent:
ServletRequest getServletRequest()
ServletContext getServletContext()
- 属性监听器
当对域属性进行增、删、改时,执行的监听器一共有三个:
ServletContextAttributeListener:在ServletContext域进行增、删、改属性时调用下面方法。
public void attributeAdded(ServletContextAttributeEvent evt)
public void attributeRemoved(ServletContextAttributeEvent evt)
public void attributeReplaced(ServletContextAttributeEvent evt)
HttpSessionAttributeListener:在HttpSession域进行增、删、改属性时调用下面方法
public void attributeAdded(HttpSessionBindingEvent evt)
public void attributeRemoved (HttpSessionBindingEvent evt)
public void attributeReplaced (HttpSessionBindingEvent evt)
ServletRequestAttributeListener:在ServletRequest域进行增、删、改属性时调用下面方法
public void attributeAdded(ServletRequestAttributeEvent evt)
public void attributeRemoved (ServletRequestAttributeEvent evt)
public void attributeReplaced (ServletRequestAttributeEvent evt)
注意:需要在自定义的Listener类中继承属性监视器,并在当前项目的WebRoot/WeB-INF/web.xml中配置监听器的实现类
下面对这三个监听器的事件对象功能进行介绍:
ServletContextAttributeEvent
String getName():获取当前操作的属性名;
Object getValue():获取当前操作的属性值;
ServletContext getServletContext():获取ServletContext对象。
HttpSessionBindingEvent
String getName():获取当前操作的属性名;
Object getValue():获取当前操作的属性值;
HttpSession getSession():获取当前操作的session对象。
ServletRequestAttributeEvent
String getName():获取当前操作的属性名;
Object getValue():获取当前操作的属性值;
ServletContext getServletContext():获取ServletContext对象;
ServletRequest getServletRequest():获取当前操作的ServletRequest对象。