千家信息网

servlet session listener

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,HttpSessionListener-创建与销毁//@WebListener servlet3.0支持注解public class HttpSessionListenerDemo implement
千家信息网最后更新 2025年01月31日servlet session listener

HttpSessionListener-创建与销毁

//@WebListener servlet3.0支持注解public class HttpSessionListenerDemo implements HttpSessionListener{    @Override    public void sessionCreated(HttpSessionEvent httpSessionEvent) {        HttpSession session = httpSessionEvent.getSession();        ServletContext servletContext = session.getServletContext();        // undo    }    @Override    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {        // undo    }}

HttpSessionActivationListener-顿化与激活

//@WebListener servlet3.0支持注解public class HttpSessionActivationListenerDemo implements HttpSessionActivationListener {    @Override    public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {    }    @Override    public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {    }}

HttpSessionAttributeListener-属性值变化

//@WebListener servlet3.0支持注解public class HttpSessionAttributeListenerDemo implements HttpSessionAttributeListener {    @Override    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {    }    @Override    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {    }    @Override    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {    }}

HttpSessionBindingListener-属性值删除或增加

//@WebListener servlet3.0支持注解public class HttpSessionBindingListenerDemo implements HttpSessionBindingListener {    @Override    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {    }    @Override    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {    }}

设计架构
监听器的设计模式是观察者模式。
事件源:HttpSession
事件:EventObject的子类,如:HttpSessionEvent和HttpSessionBindingEvent
事件监听器:EventListener的子类。

被观察者是servlet容器。观察者则是我们写的listener.通常在自已实现的listener中都会引入实际的观察者,来处理事件。

说明:web.xml的加载顺序是:【Context-Param】->【Listener】->【Filter】->【Servlet】,而同个类型之间的实际程序调用的时候的顺序是根据对应的Mapping的顺序进行调用。

0