Three components of javaWeb:
Servlet
Listener
Filter
This blog mainly talks about Listener listener.
Monitor:
- The listener is an interface, and its specific content is implemented according to its own needs.
- Methods in the listener are called when a particular event occurs
- It needs to be registered
A brief introduction of listener in Java Web
The event sources monitored in Java Web are: ServletContext, HttpSession, and ServletRequest, which are three domain objects.
Listeners listening to the creation and destruction of domain objects;
Listener listening to the domain object "operation domain attribute";
Listener listening to HttpSession.
-
ServletContext
-
Declaration cycle listener: ServletContextListener, which has two methods, one is called at birth and the other is called at death.
Void contextinitialized9 (servletcontextevent SCE): called when creating a ServletContext
void ContextDestoryed(ServletContextEvent sce): called when destroying Servletcontext -
Attribute listening: ServletContextAttributeListener, which has three methods: one is called when adding attributes, one is called when replacing attributes, and the last is called when removing attributes
Void attributeadded (servletcontextattributeevent event): called when adding an attribute
void attributeReplaced(ServletContextAttributeEvent event): called when replacing an attribute
void attributeRemoved(ServletContextAttributeEvent event): called when an attribute is removed
-
-
HttpSession
-
Life cycle monitoring: HttpSessionListener, which has two methods, one is called at birth and the other is called at death
void sessionCreated(HttpSessionEvent se): called when creating a session
Void sessiondestroyed (httpsessionevent SE): called when destroying a session -
Attribute listening: HttpSessionAttributeListener, which is called when adding attribute, deleting attribute and replacing attribute.
void attributeAdded(HttpSessionBindingEvent event): called when adding an attribute
void attributeReplaced(HttpSessionBindingEvent event): called when replacing an attribute
void attributeRemoved(HttpSessionBindingEvent event): remove attribute
-
-
ServletRequest
- Life cycle: ServletRequestListener, which has two methods: one is called at birth, the other is called at death;
void requestInitialized(ServletRequestEvent sre): when creating a request
void requestDestroyed(ServletRequestEvent sre): when destroying a request- Attribute monitoring
void attributeAdded(ServletRequestAttributeEvent srae): when adding an attribute
void attributeReplaced(ServletRequestAttributeEvent srae): when replacing an attribute
void attributeRemoved(ServletRequestAttributeEvent srae): when an attribute is removed
Writing listener in Java Web
Write a listener class: it is required to implement a listening interface.
② configure in web.xml to complete registration
Introduction to event object
ServletContextEvent: ServletContext getServletContext()
HttpSessionEvent: HttpSession getSession()
ServletRequest:
①: ServletContext getServletContext()
②: ServletReques getServletRequest()
ServletContextAttributeEvent:
>String getName(): Gets the property name of the current operation;
>Object getValue(): Get the property value of the current operation;
>ServletContext getServletContext(): Obtain ServletContext Object.
HttpSessionBindingEvent
>String getName(): Gets the property name of the current operation;
>Object getValue(): Get the property value of the current operation;
>HttpSession getSession(): Get the session Object.
ServletRequestAttributeEvent
>Object getValue(): Get the property value of the current operation;
>ServletContext getServletContext(): Obtain ServletContext Object;
>ServletRequest getServletRequest(): Get the ServletRequest Object.
public class MyServletContextListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent evt) { System.out.println("Destruction ServletContext object"); } public void contextInitialized(ServletContextEvent evt) { System.out.println("Establish ServletContext object"); } }
public class MyHttpSessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent evt) { System.out.println("Establish session object"); } public void sessionDestroyed(HttpSessionEvent evt) { System.out.println("Destruction session object"); } }
public class MyServletRequestListener implements ServletRequestListener { public void requestDestroyed(ServletRequestEvent evt) { System.out.println("Destruction request object"); } public void requestInitialized(ServletRequestEvent evt) { System.out.println("Establish request object"); } }
Configuration in web.xml
<listener> < listener class > the full class name of myservercontextlistener listener < / listener class > </listener> <listener> < listener class > the full class name of the myhttpsessionlistener listener < / listener class > </listener> <listener> < listener class > the full class name of myserverrequestlistener listener < / listener class > </listener> <session-config> <! -- to see the process of session destruction, set the session expiration time to 1 second -- > <session-timeout>1</session-timeout> </session-config>
Example of attribute listening: create a class and implement the ServletContextAttributeListener,ServletRequestAttributeListener,HttpSessionAttributeListener interface at the same time
public class MyListener implements ServletContextAttributeListener, ServletRequestAttributeListener, HttpSessionAttributeListener { public void attributeAdded(HttpSessionBindingEvent evt) { System.out.println("towards session Add properties to:" + evt.getName() + "=" + evt.getValue()); } public void attributeRemoved(HttpSessionBindingEvent evt) { System.out.println("from session Remove attribute from:" + evt.getName() + "=" + evt.getValue()); } public void attributeReplaced(HttpSessionBindingEvent evt) { System.out.println("modify session Properties in:" + evt.getName() + "=" + evt.getValue()); } public void attributeAdded(ServletRequestAttributeEvent evt) { System.out.println("towards request Add properties to:" + evt.getName() + "=" + evt.getValue()); } public void attributeRemoved(ServletRequestAttributeEvent evt) { System.out.println("from request Remove attribute from:" + evt.getName() + "=" + evt.getValue()); } public void attributeReplaced(ServletRequestAttributeEvent evt) { System.out.println("modify request Properties in:" + evt.getName() + "=" + evt.getValue()); } public void attributeAdded(ServletContextAttributeEvent evt) { System.out.println("towards context Add properties to:" + evt.getName() + "=" + evt.getValue()); } public void attributeRemoved(ServletContextAttributeEvent evt) { System.out.println("from context Remove attribute from:" + evt.getName() + "=" + evt.getValue()); } public void attributeReplaced(ServletContextAttributeEvent evt) { System.out.println("modify context Properties in:" + evt.getName() + "=" + evt.getValue()); } }
The configuration in web.xml is the same as the above configuration when event listening. The only change is that the full class name changes to the full class name of MyListener.