Listener is a Servlet Listener that can listen to client requests and server operations. Through the listener, some operations can be automatically triggered, such as the number of online users. When an Http Session is added, the session Created (Http Session Event se) method is triggered, so that 1 can be added to the number of online users. Common listening interfaces include the following:
Servlet Context Attribute Listener listens for operations on Servlet Context attributes, such as adding, deleting, and modifying attributes.
Servlet Context Listener listens to Servlet Context. When a Servlet Context is created, the Context Initialized (Servlet Context Event sce) method is stimulated. When the Servlet Context is Destroyed, the Context Destroyed (Servlet Context Event sce) method is stimulated.
The Http Session Listener listens for Http Session operations. When a Session is Created, the session Created (Http Session Event se) method is activated. When a Session is Destroyed, the session Destroyed (Http Session Event se) method is activated.
The Http Session Attribute Listener listens for the Attribute operations in the Http Session. When an attribute is Added to the Session, the attribute Added (Http Session Binding Event se) method is triggered. When an attribute is deleted in the Session, the attribute Removed (Http Session Binding Event se) method is used; when the Session attribute is reset, the attribute Replaced (Http Session Binding Event se) method is triggered.
Next we will develop a specific example where the listener can count the number of online users. When the Servlet Context is initialized and destroyed, the corresponding information is printed on the server console. When attributes in the Servlet Context are added, changed, or deleted, the corresponding information is printed on the server console.
To obtain the above functions, the listener must implement the following three interfaces:
◆ HttpSessionListener
◆ Servlet Context Listener
◆ Servlet Context AttributeListener
- Import javax. servlet. http .*;
- Import javax. servlet .*;
- Public class OnLineCountListener implements HttpSessionListener,
- ServletContextListener, ServletContextAttributeListener
- {
- Private int count;
- Private ServletContextContext=Null;
- Public OnLineCountListener ()
- {
- Count=0;
- // SetContext ();
- }
- // Triggered when a session is created
- Public void sessionCreated (HttpSessionEvent se)
- {
- Count ++;
- SetContext (se );
- }
- // Triggered when a session fails
- Public void sessionDestroyed (HttpSessionEvent se)
- {
- Count --;
- SetContext (se );
- }
- // Set the context attribute, which will stimulate the attributeReplaced or attributeAdded Method
- Public void setContext (HttpSessionEvent se)
- {
- Se. getSession (). getServletContext ().
- SetAttribute ("onLine", new Integer (count ));
- }
- // Triggered when a new attribute is added
- Public void attributeAdded (ServletContextAttributeEvent event ){
- Log ("attributeAdded ('" + event. getName () + "', '" +
- Event. getValue () + "')");
- }
- // Triggered when a new attribute is deleted.
- Public void attributeRemoved (ServletContextAttributeEvent event ){
- Log ("attributeRemoved ('" + event. getName () + "', '" +
- Event. getValue () + "')");
- }
- // Triggered when the attribute is replaced
- Public void attributeReplaced (ServletContextAttributeEvent event ){
- Log ("attributeReplaced ('" + event. getName () + "', '" +
- Event. getValue () + "')");
- }
- // Triggered when context is deleted
- Public void contextDestroyed (ServletContextEvent event ){
- Log ("contextDestroyed ()");
- This. context=Null;
- }
- // Triggered during context Initialization
- Public void contextInitialized (ServletContextEvent event ){
- This. context=Event. GetServletContext ();
- Log ("contextInitialized ()");
- }
- Private void log (String message ){
- System. out. println ("ContextListener:" + message );
- }
- }
In the OnLine Count Listener, count indicates the number of OnLine users. The OnLine Count Listener is automatically executed when the Web server is started. After the OnLine Count Listener is constructed, set count to 0. Each time a Session is added, the OnLine Count Listener automatically calls the session Created (Http Session Event se) method. Each time a Session is Destroyed, the OnLine Count Listener automatically calls session Destroyed (Http Session Event se) method. When the session Created (Http Session Event se) method is called, it indicates that another customer is requesting. At this time, the number of online users (count) is increased by 1 and the count is written to the Servlet Context. Servlet Context information is shared by all clients. In this way, each client can read the current number of online users.
In terms of scope, the Servlet has the following scopes: Servlet Context, Http Session, and Servlet Request. The Listener listens to Http Session.
- Servlet Context range
- Brief Analysis on the concept of Servlet 3.0 API
- Servlet comments and deployment descriptors
- New Features of Java Servlet and Servlet 3.0
- Potential risks of Servlet and JSP