jsp(web监听器)

jsp中的监听器分为三类:ServletContext事件监听器、HttpSession事件监听器、ServletRequest事件监听器。

一、监听ServletContext事件

ServletContext该事件是WEb应用程序生命周期事件,作用于整个web应用(相当于application作用域)
在这里插入图片描述

1.1、ServletContextListener接口

public void contextInitialized(ServletContextEvent sce):创建Servletcontext(即创建应用程序)时

public void contextDestroyed(ServletContextEvent sce):销毁Servletcontext(即销毁应用程序)时

1.2、ServletContextAttributeListener接口

public void attributeAdded(ServletContextAttributeEvent event):向应用程序添加属性时;

public void attributeReplaced(ServletContextAttributeEvent event):在应用程序中替换属性时;

public void attributeRemoved(ServletContextAttributeEvent event):在应用程序中移除属性时;

二、监听请求(ServletRequest)事件

ServletRequest 作用于 request作用域
在这里插入图片描述

2.1、ServletRequestListener接口

public void requestInitialized(ServletRequestEvent sre):创建request时

public void requestDestroyed(ServletRequestEvent sre):销毁request时

2.2、ServletRequestAttributeListener接口

public void attributeAdded(ServletRequestAttributeEvent srae):添加属性时

public void attributeReplaced(ServletRequestAttributeEvent srae):替换属性时

public void attributeRemoved(ServletRequestAttributeEvent srae):移除属性时

三、监听会话(HttpSession)事件

HttpSession作用域为会话阶段
在这里插入图片描述

3.1、HttpSessionListener接口

public void sessionCreated(HttpSessionEvent se):创建session时

public void sessionDestroyed(HttpSessionEvent se):销毁session时

3.2、HttpSessionaActivationListener接口

public void attributeAdded(HttpSessionBindingEvent event):添加属性时;

public void attributeReplaced(HttpSessionBindingEvent event):替换属性时

public void attributeRemoved(HttpSessionBindingEvent event):移除属性时

3.3、HttpSessionAttributeListener接口

public void attributeAdded(HttpSessionBindingEvent event):添加属性时;

public void attributeReplaced(HttpSessionBindingEvent event):替换属性时

public void attributeRemoved(HttpSessionBindingEvent event):移除属性时

3.4、HttpSessionBindingListener接口

public void valueBound(HttpSessionBindingEvent event);   //对象绑定到一个会话上时调用该方法  

public void valueUnbound(HttpSessionBindingEvent event); //对象从一个会话上解除绑定时调用该方法

3.5、HttpSessionAttributeListener和HttpSessionBindingListener的区别
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_38420342/article/details/83588851