Servlet session listener
HttpSessionListener- creation and destruction
/ / @ WebListener servlet3.0 supports annotations 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}}
Stunning and activation of HttpSessionActivationListener-
/ / @ WebListener servlet3.0 supports annotations public class HttpSessionActivationListenerDemo implements HttpSessionActivationListener {@ Override public void sessionWillPassivate (HttpSessionEvent httpSessionEvent) {} @ Override public void sessionDidActivate (HttpSessionEvent httpSessionEvent) {}}
HttpSessionAttributeListener- attribute value change
/ / @ WebListener servlet3.0 supports annotations public class HttpSessionAttributeListenerDemo implements HttpSessionAttributeListener {@ Override public void attributeAdded (HttpSessionBindingEvent httpSessionBindingEvent) {} @ Override public void attributeRemoved (HttpSessionBindingEvent httpSessionBindingEvent) {} @ Override public void attributeReplaced (HttpSessionBindingEvent httpSessionBindingEvent) {}}
Delete or increase the value of the HttpSessionBindingListener- attribute
/ / @ WebListener servlet3.0 supports annotations public class HttpSessionBindingListenerDemo implements HttpSessionBindingListener {@ Override public void valueBound (HttpSessionBindingEvent httpSessionBindingEvent) {} @ Override public void valueUnbound (HttpSessionBindingEvent httpSessionBindingEvent) {}}
Design architecture
The design pattern of the listener is the observer pattern.
Event source: HttpSession
Events: subclasses of EventObject, such as HttpSessionEvent and HttpSessionBindingEvent
Event listener: a subclass of EventListener.
The observed is the servlet container. The observer is our listener. Usually an actual observer is introduced into a self-implemented listener to handle events.
Note: the loading order of web.xml is: [Context-Param]-> [Listener]-> [Filter]-> [Servlet], and the order of actual program calls between the same type is based on the order of the corresponding Mapping.