First, the concept of servlet listener
A servlet listener is a special class defined in the servlet specification that listens to the creation and destruction events of domain objects such as ServletContext, HttpSession, and ServletRequest.
and listening for events in which the properties in these domain objects have been modified.
Listener object:
1, servletcontext:application, the entire application exists only one
2, httpsession:session, for each dialogue
3, Servletrequest:request, for each customer request
Listener content: Create, destroy, attribute change events
Monitoring role: Can be in the event before, after the occurrence of some processing, generally can be used to count the number of online and online users, statistics website visits, system startup initialization information.
Second, the basic use of the listener
To create a step:
1. Create a class that implements the Listener interface
2. Configure Web. xml file, Register Listener
<listener> <listener-class> Full path name </listener-class></listener>
Boot order of the listener: start in the configuration order of Web. xml
Load Order: Listener > Filter >servlet
Iii. Classification of listeners
3.1 According to the listener's object division
1. Event listener for monitoring Application Environment object (ServletContext), implement Servletcontextlistener, Servletcontextattributelistener interface
2, for listening to the user session object (httpseesion) event listener, implementation of Httpsessionlistener, Httpsessionattributelistener interface
3, for listening to the request message object (ServletRequest) event Listener, implement Servletrequestlistener, Servletrequestattributelistener interface
3.2 According to the Listening events Division
The Servletcontextlistener, Httpsessionlistener and Servletrequestlistener interfaces are implemented according to the different listener objects.
Project initialization information can be configured in Web. xml
<context-param> <param-name> Property name </param-name> <param-value> attribute value </param-value ></context-param>
Custom Listener
Public classMyfirstlistener implements servletcontextlistener{ Public voidcontextinitialized (Servletcontextevent sce) {//get the properties configured in Web. XMLString Value=sce.getservletcontext (). Getinitparameter ("Property name"); System. out. println (value); } Public voidcontextdestroyed (Servletcontextevent sce) {//Action on Shutdown}}
Web. XML config Session timeout parameter, unit: minutes, Session timeout time is not accurate
<session-config> <session-timeout></session-timeout></session-config>
Javaweb Development--servlet Listener Listener