I. Concepts
A listener is a common java program that implements a specific interface. This program is used to listen for method calls or attribute changes of another java object. After the above events occur on the Monitored object, A Method of the listener will be executed immediately.
Ii. Servle listener
1. Compile the Servlet listener
(1) like writing other event listeners, compiling servlet listeners also requires a specific interface and overwriting the corresponding methods in the interface for the corresponding action.
(2) slightly different from other event listeners, the registration of servlet listeners is not directly registered on the event source, but is done by the WEB Container. Developers only need to register on the web. in the xml file, use the <listener> label to configure the listener. The web Container automatically registers the listener to the event source.
(3) Multiple Servlet Event Listeners can be configured in a web. xml file. The web server loads and registers these Serlvet event listeners in the order they are registered in the web. xml file.
2. multiple types of listeners are defined in the Servlet specification. The event sources used for listening are ServletContext, HttpSession, and ServletRequest domain objects.
(1) Listening to the creation and destruction of servletContext domain objects
① The ServletContextListener interface is used to listen to the creation and destruction events of the ServletContext object.
I. When the ServletContext object is created, the contextInitialized (ServletContextEvent sce) method is activated.
II when the ServletContext object is destroyed, the contextDestroyed (ServletContextEvent sce) method is stimulated.
② When to create and destroy the servletContext domain object:
I create: server startup creates servletcontext for each web Application
Ii. Destruction: Disable servletContext that represents each web application before the server is shut down.
(2) Listen to HttpSession domain object creation and destruction
① The HttpSessionListener interface is used to listen to the creation and destruction of HttpSession
When I create a Session, the sessionCreated (HttpSessionEventse) method will be called.
Ii. When a Session is destroyed, the sessionDestroyed (HttpSessionEventse) method will be called.
② Creation of Session domain object and destruction time: the server creates a session during each access.
Destroy: if the user's session is not used for 30 minutes, the server will destroy the session. We can also configure the session expiration time in web. xml.
(3) Listen to the HttpRequest domain object creation and destruction
① The ServletRequestListener interface is used to listen to the creation and destruction of the ServletRequest object.
When I Request object is created, the listener's requestInitialized method will be called.
Ⅱ when the Request object is destroyed, the listener's requestDestroyed method will be called.
② When the servletRequest domain object is created and destroyed:
I create: A reqeust is created for each access.
Ii. Destruction: when the current access ends, the request object will be destroyed.
3. Servlet specifications for the operations on these three objects, and divide these types of listeners into three types.
(1) Listen to event listeners for the creation and destruction of three domain objects
(2) Listener for adding and deleting attributes of domain objects
(3) Listen to Event Listeners bound to an object in the HttpSession domain.
Application Cases of Servlet listeners in development:
4. Listen for attribute changes of three domain objects
(1) The Servlet specification defines the listener for listening to attribute change information events in ServletContext, HttpSession, and HttpServletRequest objects.
(2) The three listener interfaces are ServletContextAttributeListener and HttpSessionAttributeListener ServletRequestAttributeListener.
(3) The three interfaces define three methods to process the events of adding, deleting, and replacing attributes in the listened object, the method names of the same event in the three interfaces are identical, but the accepted parameter types are different.
5. attributeAdded Method
(1) When an attribute is added to the listener object, the web Container calls the attributeAdded method of the event listener to respond accordingly. This method accepts an event-type parameter, the listener can use this parameter to obtain the domain object that is adding attributes and the attribute object that is saved to the domain.
(2) The complete Syntax of each domain property listener is defined:
I public voidattributeAdded (ServletContextAttributeEvent scae)
II public void attributeAdded (HttpSessionBindingEvent hsbe)
III public voidattributeAdded (ServletRequestAttributeEvent srae)
6. attributeRemoved Method
(1) When a property in the Monitored object is deleted, the web Container calls the event listener method to perform the corresponding operation.
(2) The complete Syntax of each domain property listener is defined:
① Public voidattributeRemoved (ServletContextAttributeEvent scae)
② Public void attributeRemoved (HttpSessionBindingEvent hsbe)
③ Public void attributeRemoved (ServletRequestAttributeEvent srae)
7. attributeReplaced Method
(1) When an attribute in the listener's domain object is replaced, the web Container calls the event listener's method to perform the corresponding
(2) The complete Syntax of each domain property listener is defined:
① Public voidattributeReplaced (ServletContextAttributeEvent scae)
② Public void attributeReplaced (HttpSessionBindingEvent hsbe)
③ Public void attributeReplaced (ServletRequestAttributeEvent srae)
Listener
A listener is a common java program that implements a specific interface. This program is used to listen for method calls or attribute changes of another java object. After the above events occur on the Monitored object, A Method of the listener will be executed immediately.
Typical Case of listener: listening for event listeners in the window
Servle listener
Multiple types of listeners are defined in the Servlet specification. The event sources used for listening are ServletContext, HttpSession, and ServletRequest domain objects.
The Servlet specification divides these types of listeners into three types for operations on these three objects.
Monitors event listeners for the creation and destruction of three domain objects
Added or deleted event listeners in the listener domain object.
Listen to the event listener for the status of an object bound to the HttpSession domain. (View API Documentation)
Monitors creation and destruction of servletContext domain objects
The ServletContextListener interface is used to listen to the creation and destruction events of the ServletContext object.
When the ServletContext object is created, the contextInitialized (ServletContextEvent sce) method is activated.
When the ServletContext object is destroyed, the contextDestroyed (ServletContextEvent sce) method is triggered.
Question: When will the servletContext domain object be created and destroyed:
Create: server startup creates servletcontext for each web Application
Destroy: Shut down the servletContext that represents each web application before the server is shut down.
Compile Servlet listener
Like writing other event listeners, compiling servlet listeners also requires a specific interface and overwriting the corresponding methods in the interface for the corresponding action.
Unlike other event listeners, servlet listeners are registered not directly on the event source, but by WEB containers. Developers only need to register them on the web. in the xml file, use the <listener> label to configure the listener. The web Container automatically registers the listener to the event source.
Multiple Servlet Event Listeners can be configured in a web. xml file. The web server loads and registers these Serlvet event listeners in the order they are registered in the web. xml file.
Listen to HttpSession domain object creation and destruction
The HttpSessionListener interface is used to listen for HttpSession creation and destruction.
When you create a Session, the sessionCreated (HttpSessionEvent se) method will be called.
When a Session is destroyed, the sessionDestroyed (HttpSessionEvent se) method will be called.
The time when the Session domain object is created and destroyed: the server creates a session during each access.
Destroy: if the user's session is not used for 30 minutes, the server will destroy the session. We can also configure the session expiration time in web. xml.
The ServletRequestListener interface is used to listen to the creation and destruction of the ServletRequest object.
When a Request object is created, the listener's requestInitialized method will be called.
When the Request object is destroyed, the listener's requestDestroyed method will be called.
(Review the request object here. Refresh the access servlet multiple times in the browser window, view the creation and destruction of the request object, write a servlet, and then jump to other servlets using sendRedirect and forward methods, view the creation and consumption of the request object)
When the servletRequest domain object is created and destroyed:
Create: A reqeust is created for each access.
Destroy: the current access ends, and the request object will be destroyed.
Application Case of Servlet listener in development: count the number of online users
[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> My JSP 'index. jsp 'starting page </title>
</Head>
<Body>
Current website traffic: $ {applicationScope. count}
</Body>
</Html>
[Java]
Package com. hbsi. web. listener;
Import javax. servlet. ServletContext;
Import javax. servlet. http. HttpSessionEvent;
Import javax. servlet. http. HttpSessionListener;
Public class CountLineListener implements HttpSessionListener {
@ Override
Public void sessionCreated (HttpSessionEvent se ){
ServletContext context = se. getSession (). getServletContext ();
Integer count = (Integer) context. getAttribute ("count ");
If (count = null ){
Count = 1;
Context. setAttribute ("count", count );
} Else {
Count ++;
Context. setAttribute ("count", count );
}
}
@ Override
Public void sessionDestroyed (HttpSessionEvent se ){
ServletContext context = se. getSession (). getServletContext ();
Integer count = (Integer) context. getAttribute ("count ");
Count --;
}
}
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5"
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Listener>
<Listener-class> com. hbsi. web. listener. CountLineListener </listener-class>
</Listener>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>