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的顺序进行调用。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。