servletsessionlistener

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的顺序进行调用。


当前名称:servletsessionlistener
网页地址:http://ybzwz.com/article/jcpssi.html