Using the Listener class
When a Java Web application runs in a Web container, various events occur inside the Java Web application, such as the start, pause, destroy, etc. of the Web application. and session start and end in Web applications
These web apps are often invisible to developers. In fact, in the SERVLETAPI. Provides a number of listeners to listen to Web App events, where the listener class is the most common. This class allows events to be dropped when a Web internal event occurs
The listener's method.
2.Listener Foundation.
When using listener in a Java Web application, you need to define the listener implementation class and then configure it through Annoation or in the Web. xml File Listener
Programming with AWT events is exactly the same. The following is the interface of the Web Event listener.
1) Servletcontextlistener monitor web App startup and shutdown
2) Servletcontextattributelistener monitoring ServletContext The change in the range of properties
3) Servletrequestlistener Monitor user's request
4) Servletrequestattributelistener Monitor the change of properties within the ServletRequest range (request)
5) Httpsessionlistener Monitor user session start and end
6) Httpsessionattributelistener Monitor the change of the attributes within the session within the HttpSession range.
The filter filter, if there is a page configured with multiple filters, whose execution order is, in the order of configuration in Web. XML, the first to execute the completed filtering is the last configured filter.
Instance one, create a Servletcontex listener.
Servletcontextlistener is the most commonly used listener that can be loaded in the current Web application
Initializes the associated resources for the Web app: Creates a database connection pool, creates a spring IOC container, and reads the initialization parameters of the current Web application
The steps are as follows:
1. Create a class Helloservletcontextlistener implement the Servletcontextlistener interface
Two methods included in the two interface at night.
2. Configure the Listener.
<!--configuration Listener--
One, Context listener (ServletContext object Listener)
1. Context creation Destroy listener: * (when the server starts, that is, the servlet container (Tomcat) Executes the method created, that is, the Application object is created when the server starts)
(When the server shuts down, the servlet container (Tomcat) executes the method of destruction, and the Application object is destroyed)
Usage: For example, you can load the content you want to load first when the server starts < But if there are too many things loaded at this time, the server is very stressful and the boot speed will be slower >
2. Context container monitoring (when storing, adding, deleting things to application, it will trigger the listener's three methods respectively)
Configure the Listener: (The following two configurations are the same)
Second, session monitoring (HttpSession object Listener)
1. Session Creation Destroy Listener: * (session is created when browser is started) (Session container is created) (sessions are destroyed when browser is started)
.2. Session Container Monitoring
3 Session Binding Monitoring
Third, request listener (ServletRequest object Listener)
1. Request to create a destroy listener: * * (when the second time access to something inside a request container, because the container has been destroyed, so access does not exist)
.2. Request Container Monitoring
Primary Knowledge java-Listener