Java Web development-Servlet listener and servlet listener
1. Servlet listener Concept
The Servlet listener is a special class defined in the Servlet specification. It is used to listen to the creation and destruction events of domain objects such as ServletContext, HttpSession, and ServletRequest, and events that listen to the modification of attributes in these domain objects.
Listener object:
1. ServletContext: application. Only one application exists.
2. HttpSession: session, for each conversation
3. ServletRequest: request, for each customer request
Listener content: Create, destroy, and attribute change events
Listener: it can be used to process events before or after an event. Generally, it can be used to count the number of online users and users, Count website visits, and initialize the system at startup.
Ii. basic use of listeners
Procedure:
1. Create a class to implement the listener Interface
2. Configure the web. xml file and register the listener.
<Listener> <listener-class> complete class name </listener-class> </listener>
Listener startup sequence: Start according to the web. xml configuration order
Loading Sequence: Listener> filter> Servlet
Iii. Listener Classification
3.1 divided by listening objects
1. It is used to listen to the event listener of the application environment object (ServletContext) and implement the ServletContextListener and ServletContextAttributeListener interfaces.
2. used to listen to the event listener of the user session object (httsesesion) and implement the HttpSessionListener and HttpSessionAttributeListener Interfaces
3. It is used to listen to the event listener of the request message object (ServletRequest) and implement the interfaces of ServletRequestListener and ServletRequestAttributeListener.
3.2 divided by listener events
1. listener for event listeners for domain object creation and destruction
The interfaces of ServletContextListener, HttpSessionListener, and ServletRequestListener are implemented based on different listening objects.
① Creation and destruction of ServletContext: contextInitialized method and contextDestroyed Method
Public void contextInitialized (ServletContextEvent sce) // call public void contextDestroyed (ServletContextEvent sce) when the ServletContext is created // call the call when the ServletContext is destroyed
Main purposes: as a timer, loading global attribute objects, creating global database connections, loading cache information, etc.
Instance:
You can configure project initialization information in web. xml and start it in the contextInitialized method.
<Context-param> <param-name> attribute name </param-name> <param-value> attribute value </param-value> </context-param>
Custom listener
Public class MyFirstListener implements ServletContextListener {public void contextInitialized (ServletContextEvent sce) {// obtain the web. string value = sce. getServletContext (). getInitParameter ("property name"); System. out. println (value);} public void contextDestroyed (ServletContextEvent sce) {// operation upon shutdown }}
② HttpSession creation and destruction: sessionCreated and sessionDestroyed Methods
Public void sessionCreated (HttpSessionEvent se) // call public void sessionDestroyed (HttpSessionEvent se) when the session is created // call it when the session is destroyed
Main Purpose: count the number of online users and record access logs.
[Note]
Set the session Timeout parameter in web. xml. Unit: minute. The session timeout time is not accurate.
<session-config> <session-timeout>10</session-timeout></session-config>
③ Creation and destruction of ServletRequest: requestInitialized and requestDestroyed Methods
Public void requestInitialized (ServletRequestEvent sre) // call public void requestDestroyed (ServletRequestEvent sre) when the request is created // call when the request is destroyed
Main Purpose: Read request Parameters and record access history
Instance:
Public class MySRequestListener implements SevletRequestListener {public void requestInitialized (ServletRequestEvent sre) {String value = sre. getServletRequest (). getParameter ("key"); // obtain the System parameter in the request. out. println (value);} public void requestDestroyed (ServletRequestEvent sre) {System. out. println ("request destroyed ");}}
2. Listening for added and deleted event listeners in domain objects
The ServletContextAttributeListener, HttpSessionAttributeListener, and ServletRequestAttributeListener interfaces are implemented based on different listening objects.
Implementation Methods: attributeAdded, attributeRemoved, and attributeReplaced
3. Listen to the event listener (create a normal JavaBean) bound to the status of an object in the httsponesion domain)
Object status in HttpSession: Binding → unbinding; passivation → Activation
Implementation interface and Method: HttpSessionBindingListener interface (valueBound and valueUnbound methods), HttpSessionActivationListener interface (sessionWillPassivate and sessionDidActivate methods)
[NOTE 1] ① the Serializable interface must be implemented for passivation and activation.
② You do not need to register in web. xml
[NOTE 2]
BIND: save to the session object through setAttribute
Unbind: Remove through removeAttribue
Passivation: persists session objects to storage devices.
Activation: Restores session objects from storage devices.
Session passivation mechanism:
① Temporarily serialize session objects that are not frequently used by the server to system files or databases, and deserialize them to the memory during use. The whole process is automatically completed by the server;
② The session passivation mechanism is managed by SessionManager. To create a normal JavaBean binding and contact, you must implement the HttpSessionBindingListener interface.
Iv. Use of listeners in Servlet3.0
4.1 conditions of use
1. Use the new standard jar package of servlet3.0
2. JDK 1.6 or later
3. the compiler compilation level is 6.0
4. Use the 3.0 specification in the web. xml file
5. Use a web container that supports the servlet3.0 feature, such as tomcat7
4.2 listener usage in servlet3.0
You can use the annotation @ WebListener to define the sequence of listeners.
@ WebListener common attributes
Attribute name |
Type |
Optional |
Description |
Value |
String |
Yes |
Description of the listener |
@WebListener("This is a listener")public class FirstListener impliements ServletRequestListener{}
This annotation is used to declare a class as a listener. The class marked by @ WebListener must implement at least one of the following interfaces:
ServletContextListener
ServletContextAttributeListener
ServletRequestListener
ServletRequestAttributeListener
HttpSessionListener
HttpSessionAttributeListener
[JAVA Web Development Technology Application -- listener] http://www.imooc.com/learn/271