码迷,mamicode.com
首页 > 其他好文 > 详细

servlet session listener

时间:2018-05-22 22:25:49      阅读:153      评论:0      收藏:0      [点我收藏+]

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

servlet session listener

标签:servlet listener

原文地址:http://blog.51cto.com/881206524/2119229

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!