Three major java Web Components: Listeners and three major java Web listeners
Listener Overview
Listener (Listener) is a special Servlet technology that can listen to the context information, Servlet request information, and Servlet session information of Web applications, that is, ServletContext, ServletRequest, and HttpSession. The corresponding processing program is called in the background according to different situations. Use listeners to monitor and control Web applications to enhance the event processing capability of Web applications.
Listeners use a group of listening interfaces and event classes in javax. servlet. jar. Listeners can be divided into three types based on different listening objects:
1) ServletContext event listener: used to listen to application environment objects.
2) HttpSession event listener: used to listen to user session objects.
3) ServletRequest event listener: used to listen to request message objects.
The three listeners contain eight listening interfaces and six listening event classes.
Listener object |
Listener Interface |
Listener events |
ServletRequest |
ServletRequestListener |
ServletRequestEvent |
ServletRequestAttributeListener |
ServletRequestAttributeEvent |
HttpSession |
HttpSessionListener |
HttpSessionEvent |
HttpSessionActivationListener |
HttpSessionAttributeListener |
HttpSessionBindingEvent |
HttpSessionBindingListener |
ServletContext |
ServletContextListener |
ServletContextEvent |
ServletContextAttributeListener |
ServletContextAttributeEvent |
Listener startup sequence
1) when there are multiple listeners, start them sequentially according to the order configured by each listener in web. xml.
2) The Starting sequence of the filter and Servlet is as follows:
Listener> filter> Servlet
Listener category
1) Listen to the ServletContext object (corresponding to the jsp built-in object -- application Object)
Listener on the ServletCOntext object. You can listen to the attribute changes in the ServletContext object (add, delete, modify, and delete operations), or the changes (create and destroy) of the ServletContext object ). Common listening methods:
Interface Name |
Interface Method |
Excitation condition |
ServletContextAttributeListener |
Public void attributeAdded (ServletContextAttributeEvent scab ); |
Add attribute |
Public void attributeRemoved (ServletContextAttributeEvent scab ); |
Delete attributes |
Public void attributeRepalced (ServletContextAttributeEvent scab ); |
Modify attributes |
ServletContextListener |
Public void contextInitialized (ServletContextEvent sce ); |
Create object |
Public void contextDestroyed (ServletContextEvent sce ); |
Destroy object |
2) Listener session (HttpSession object) (corresponding to the jsp built-in object -- session Object ):
Listen to the HttpSession object. You can listen to the attribute changes (add, delete, modify, and delete operations) in the HttpSession object or the changes (create and destroy) of the HttpSession object ), and the status of this object. You can also monitor whether the HttpSession object is bound to this monitor object. Common listening methods:
Interface Name |
Interface Method |
Excitation condition |
HttpSessionAttributeListener |
Public void attributeAdded (HttpSessionBindingEvent se ); |
Add attribute |
Public void attributeRemoved (HttpSessionBindingEvent se ); |
Delete attributes |
Public void attributeReplaced (HttpSessionBindingEvent se ); |
Modify attributes |
HttpSessionListener |
Public void sessionCreated (HttpSessionEvent se ); |
Create object |
Public void sessionDestroyed (HttpSessionEvent se ); |
Destroy object |
HttpSessionActivationListener |
Public void sessionDidActivate (HttpSessionEvent se ); |
The session has just been activated. |
Public void sessionWillPssivate (HttpSessionEvent se ); |
Session to be passive |
HttpSessionBindingListener |
Public void valueBound (HttpSessionBindingEvent se ); |
Call setAttribute () |
Public void valueUnbound (HttpSessionBindingEvent se ); |
Call removeAttribute () |
Note 1:
Activate and Passivate are measures taken by Web containers to better utilize system resources or balance server load.
Session Object passivation is to temporarily store the session object to the hard disk through serialization;
In contrast to passivation, activation re-loads the session objects stored on the hard disk to the Web container.
Two kinds of passive manager in Tomcat: org. apache. catalina. session. StandardManger and org. apache. catalina. session. Persistentmanger
NOTE 2:
There are two scenarios for session destruction:
(1) session Timeout, web. xml configuration:
<Session-config> <session-timeout> 120 </session-timeout> <! -- Session120 minutes later --> </session-config>
(2) manually invalidate the session
Public void invalidate (); // method used to invalidate a session. Session. invalidate ();
3) Listener request (ServletRequest object) (corresponding to jsp built-in object -- request object)
Listening to the ServletRequest object can listen to changes in attributes of the ServletRequest object (add, delete, modify, and delete operations), or changes to the ServletRequest object itself (create and destroy ). Common listening methods:
Interface Name |
Interface Method |
Excitation condition |
ServletRequestAttributeListener |
Public void attributeAdded (ServletRequestAttributeEvent srae ); |
Add attribute |
Public void attributeRemoved (ServletRequestAttributeEvent srae ); |
Delete attributes |
Public void attributeReplaced (ServletRequestAttributeEvent srae ); |
Modify attributes |
ServletRequestListener |
Public void requestInitialized (ServletRequestEvent sre ); |
Create object |
Public void requestDestroyed (ServletRequestEvent sre ); |
Destroy object |
Listener design procedure (Instance)
Instance: counts the number of online users.
When a browser accesses the website for the first time, the server creates an HttpSession object and triggers an HttpSession creation event. If an HttpSessionListener event listener is registered, the server calls the sessionCreated method of the HttpSessionListener event listener. On the contrary, when the browser user logs out or the access times out, the server will destroy the corresponding HttpSession object, trigger the HttpSession destruction event, and call the sessionDestroyed method of the registered HttpSessionListener event listener. In this way, you only need to add 1 to the counter in the sessionCreated method of the HttpSessionListener implementation class, and reduce the counter by 1 in the sessionDestroyed method to achieve the online count statistics function of the website. (There are other ways to count the number of online users in the reference blog. I will not go into details here)
Create a listener class that implements the corresponding interface:
Package com. listener; import javax. servlet. http. httpSessionEvent; import javax. servlet. http. httpSessionListener; public class OnlineListener implements HttpSessionListener {// defines the number of online users of static variable storage private static int onlineCount = 0; @ Override public void sessionCreated (HttpSessionEvent arg0) {onlineCount ++ ;} @ Override public void sessionDestroyed (HttpSessionEvent arg0) {if (onlineCount> 0) onlineCount --;} // customize the public static int getOnlineCount () method to return the number of online users () {return onlineCount ;}}
Configure the web. xml file (the configuration is relatively simple. You only need to configure the class where the listener is located, without configuring the ing address)
In servlet 3.0 and later versions, you can use annotations to declare the class that implements a listener interface as a listener, that is, Mark @ WebListener on the class name.
<! -- Configure the listener --> <listener-class> com. listener. OnlineListener </listener-class> </listener>
Create a jsp page showing the number of online users (note that you need to import the package of the listener class)
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% @ page import = "com. listener. * "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Then, you can use different browsers to access this URL and view the running result.
Reference blog: http://www.cnblogs.com/coderland/p/5902874.html
Reference books: Java EE Framework development technology and Case Study