Java Web Listener
I. Event Source: three major domains!
-
- Life Cycle listener: ServletContextListener, which has two methods: one called at creation and the other called at destruction;
-
-
- Void contextInitialized (ServletContextEvent sce): When you create a Servletcontext
- Void contextDestroyed (ServletContextEvent sce): When Servletcontext is destroyed
-
- Attribute listening: ServletContextAttributeListener. It has three methods: one called when adding an attribute, one called when replacing the attribute, and the other called when removing the attribute.
-
-
- Void attributeAdded (ServletContextAttributeEvent): when adding an attribute;
- Void attributeReplaced (ServletContextAttributeEvent event): When the attribute is replaced;
- Void attributeRemoved (ServletContextAttributeEvent event): When the attribute is removed;
-
- Lifecycle listener: HttpSessionListener. It has two methods: one called at creation and the other called at destruction;
-
-
- Void sessionCreated (HttpSessionEvent se): When a session is created
- Void sessionDestroyed (HttpSessionEvent se): When the session is destroyed
- Property listening: HttpSessioniAttributeListener. It has three methods: one called when adding an attribute, one called when replacing the attribute, and the other called when removing the attribute.
- Void attributeAdded (HttpSessionBindingEvent event): when adding an attribute;
- Void attributeReplaced (HttpSessionBindingEvent event): When the attribute is replaced
- Void attributeRemoved (HttpSessionBindingEvent event): When the attribute is removed
-
- Life Cycle listener: ServletRequestListener, which has two methods: called at creation and destroyed;
-
-
- Void requestInitialized (ServletRequestEvent sre): when a request is created
- Void requestDestroyed (ServletRequestEvent sre): when the request is destroyed
-
- Property listening: ServletRequestAttributeListener, which has three methods: one called when adding an attribute, one called when replacing the attribute, and the other called when removing the attribute.
-
-
- Void attributeAdded (ServletRequestAttributeEvent srae): when adding an attribute
- Void attributeReplaced (ServletRequestAttributeEvent srae): When the attribute is replaced
- Void attributeRemoved (ServletRequestAttributeEvent srae): When the attribute is removed
2. Complete listener compilation in Java Web:
- Write a listener class: A listener interface must be implemented;
- Registration is configured in web. xml to complete registration!
1 package web. listener; 2 3 import javax. servlet. servletContextEvent; 4 import javax. servlet. servletContextListener; 5 import javax. servlet. annotation. webListener; 6 7/** 8 * ServletContext listener 9 * can store some code to be completed when Tomcat is started! 10 **/11 @ WebListener () 12 public class AListener implements ServletContextListener {13 @ Override14 public void contextInitialized (ServletContextEvent sce) {15 System. out. println ("wow, I'm coming"); 16} 17 18 @ Override19 public void contextDestroyed (ServletContextEvent sce) {20 System. out. println ("wow, goodbye"); 21} 22}
Iii. Event objects:
- ServletContextEvent: ServletContext getServletContext ()
- HttpSessionEvent: HttpSession getSession ()
- ServletRequest:
- ServletContext getServletContext ();
- ServletReques getServletRequest ();
- ServletContextAttributeEvent:
-
- ServletContext getServletContext ();
- String getName (): Get the property name
- Object getValue (): Get the property value
- HttpSessionBindingEvent: omitted
- ServletRequestAttributeEvent: omitted
4. listener (all associated with HttpSession)
- It is used to add to the JavaBean, rather than to the three domains!
- Both listeners do not need to be registered in web. xml!
HttpSessionBindingListener: add it to the javabean, And the javabean will know whether it is added to the session.