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的顺序进行调用。
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/191722.html