Using listener requires only two steps:
- Defines the listener implementation class.
- Configure listener through annotation or in the Web. xml file
Implementing the Listener Class
Listeners that listen to different Web events are not the same, and the common Web Event listener interface is as follows:
- Servletcontextlistener: Used to listen for web App startup and shutdown.
- Servletcontextattributelistener: Used to listen for changes in the properties of the ServletContext range (application).
- Servletrequestlistener: Used to listen for user requests.
- Servletrequestattributelistener: Used to listen for changes in the properties of the ServletRequest range (request).
- Httpsessionlistener: Used to listen to the beginning and end of the user session.
- Httpsessionattributelistener: Used to listen for changes in the properties of the Httpsesssion range (session).
Taking Servletcontextlistener as an example, to implement this interface, two methods need to be implemented:
- Contextinitialized (Servletcontextevent SCE): When you start a web app, the system calls the listener method.
- Contextdestroyed (Servletcontextevent SCE): When you close a web app, the system calls the listener method.
The role of Servletcontextlistener is somewhat similar to the Load-on-startup Servlet, which can be used for callback methods to launch some background programs when the Web App starts, which are responsible for providing support for system operation.
Example: Create a listener to get the database connection at startup, set to application, close the connection when the app is closed.
@WebListener Public classGetconnlistenerImplementsservletcontextlistener{//application Startup Yes, the method is called Public voidcontextinitialized (Servletcontextevent sce) {Try{ //get an application ServletContext instanceServletContext application =Sce.getservletcontext (); //getting database information from configuration parametersString Driver = application.getinitparameter ("Driver"); String URL= Application.getinitparameter ("url"); String User= Application.getinitparameter ("User"); String Pass= Application.getinitparameter ("Pass"); //Registration DriverClass.forName (driver); //Get database connectionConnection conn =drivermanager.getconnection (Url,user,pass); //to set a database connection to a property in the application rangeApplication.setattribute ("conn", conn); }Catch(excetpion e) {e.printstacktrace (); } } //when the application is closed, the method is called Public voidcontextdestroyed (Servletcontextevent sce) {//get the ServletContext instance of the appServletContext application =Sce.getservletcontext (); Connection Conn= (Connection) application.getattribute ("conn"); //To close a database connection if(conn!=NULL){ Try{conn.close (); }Catch(SQLException e) {e.printstacktrace (); } } }}
Configure Listener
Configuring listener simply registering the listener implementation class with the Web app eliminates the need for configuration parameters and things like that. There are two ways to configure listener for a web app:
- Implement the class using the @weblistener adornment listener.
- Configured with the <listener.../> element in the Web. XML document.
You typically do not need to specify any properties when using @weblistener, as long as you use the annotation adornment listener implementation class to register the listener with the Web App.
When configured with the <listener.../> element in Web. XML, simply configure its implementation class, such as:
< Listener > <!-- -<listener-class>luxl. Getgonnlistener</listener-class></listener >
Using Servletcontextattributelistener
The Servletcontextattributelistener is used to listen for changes in the ServletContext (application) range of properties, and the listener implementing the interface requires three methods:
- Attributeadded (Servletcontextattributeevent event), which is used when the program puts an attribute into the application range.
- Attributeremoved (Servletcontextattributeevent event) when the program removes a property from the application scope, the method is started.
- Attributereplaced (Servletcontextattributeevent event), which is used when the program replaces an attribute in the application range.
Using Servletrequestlistener and Servletrequestattributelistener
Servletrequestlistener is used to listen to the arrival of a user request, the listener implementing the interface needs to implement the following two methods:
- Requestinitialized (servletrequestevent SRE); user request in the end, is initialized is the method of departure.
- Requestdestroyed (Servletrequestevent SRE); This method is triggered when a user request ends and is destroyed.
The Servletrequestattributelistener is used to listen for changes in the ServletRequest (request) range, and the listener that implements the interface needs to implement attributeadded, Three methods of attributeremoved and attributereplaced.
The application can use a listener to listen to more than one event, as long as the listener implements a class to implement multiple listener interfaces at the same time, the implementation of Servletrequestlistener, Servletrequesetattributelistener.
Using Httpsessionlistener and Httpsessionattributelistener is very similar to using Servletrequestlistener and Servletrequestattributelistener 。
Httpsessionlistener is used to monitor the creation and destruction of the user session, and the listener implementing the interface needs to implement:
- Sessioncreate (httpsessionevent se); The method is triggered when a user-server session starts and is created.
- sessiondestroyed (httpsessionevent se); The method is triggered when a user's session with the server is disconnected and destroyed.
Creation of listener in the Web