https://www.imooc.com/video/5664
The Web listener is provided by the Servlet specification, and can monitor client requests and server operations, that is, monitor ServletContext, HttpSession, and ServletRequest objects. (These three objects are called domain objects). The monitoring events are divided into the creation of these three objects, The destruction event and the change event of these three object properties. And the time to monitor the state of an object bound to the HttpSession domain, different listener interfaces should be implemented respectively.
The purpose of the web listener is as follows:<br> 1: Count the number of people online and online users - know how many users are currently using the system<br> 2: Load initialization information when the system starts - this function can be used in many ways Implementation, this is one of them<br> 3: Count the traffic of the website - similar to 1<br> 4: Combine with Spring
When there are multiple listeners, start in the order of registration in web.xml
Listeners > Filters > servlet startup order
@Listener classification: 1. According to the object of the listener: a User listens to the event listener of the application environment object (ServletContext) b Event listener used to listen to the user session object (HttpSession) c Used to listen to the request message object ( ServletRequest) event listener 2. Divided by events a. An event listener that monitors the creation and destruction of the domain object itself. b. An event listener that monitors the addition and deletion of attributes in the domain object. c. event listener for the state of the object
HttpSession creation and destruction
Implement the HttpSessionListener interface to listen for its creation and destruction events
An HttpSession can register multiple HttpSessionListener interfaces,
There can be multiple HttpSession objects in a web application
There are two event handling methods: sessionCreated method: public void sessionCreated(HttpSessionEvent se) The sessionDestroyed method is called when the session is created: public void sessionDestroyed(HttpSessionEvent se) is called when the session is destroyed
The main purpose of HttpSessionListener:
Count the number of people online, record access logs,
Record access time, accessed ip
/** * Destroy session * 1. Shut down the server * 2. Close the browser* 3. Do not close the browser, session timeout */ Do not close the browser, session timeout
You can configure the session timeout time in web.xml <session-config> <session-timeout>0</session-timeout> </session-config> 0: The session has no timeout limit 1: The session will be destroyed after 1 minute of timeout, but Not exactly one minute
Event listener for ServletRequest
Implement the ServletRequestListener interface to monitor its creation and destruction events. A ServletRequest can register multiple ServletRequestListener interfaces.
There are two event handling methods: requestInitialized method: public void requestInitialized(ServletRequestEvent sre) When the request is created, call the parameter ServletRequestEvent to get the ServletRequest object ServletRequest sr = sre.getServletRequest() and then get the initialization parameter: sr.getInitParmeter("") requestDestroyed method: public void requestDestroyed(ServletRequestEvent sre) Called when the request is destroyed
The main purpose of ServletRequest:
read parameters
Record access history
request listens to every access request of the user
request.setAttribute("...","..."); // is an attribute stored in the request object
request.getAttribute("..."); // get only the attributes set by setAttribute
request.getParameter("..."); // Get the param set in the http:// request
Classification of listeners - divided by listening events:<br>
1. The listener is bound to the state event listener of an object in the HttpSession domain; <br>
2. Object status in HttpSession: <br>
(1) Binding: saved to the session object through setAttribute; <br>
(2) Unbind: removeAttribute; <br>
(3) Passivation: Persist the session object to the storage device; <br>
(4) Activation: restore the session object from the storage device. <br>
3. Session passivation mechanism: <br>
(1) Temporarily serialize the session objects that are not often used in the server to the system file or database, and deserialize them into memory when they are used. The whole process is automatically completed by the server. <br>
(2) The passivation mechanism of the session is managed by the SessionManager. To create a common javabean binding and unbinding, it needs to implement the HttpSessionBindingListener interface<br>
4. To achieve passivation and activation, in addition to implementing the HttpSessionActivationListener interface, you also need to implement the Serializable interface
No need to register, but still have to implement the interface