Listener Overview Listener
Real-life monitoring process:
The definition of the listener in real life
Listeners are objects that are designed to listen for and handle events or state changes that occur on other objects when the listener is changed
, take immediate action accordingly.
Web Listener
Here is the monitoring process for counting the number of users online:
Web Listener
Web listener under the servlet specification:
A Web listener is a special class defined in the servlet specification.
Web listeners are used to listen for creation and destruction events for domain objects such as ServletContext, HttpSession, and ServletRequest.
The Web listener is used to listen for events that modify the properties of a domain object.
A Web listener can make some necessary processing before an event occurs.
The main purpose of the listener
1) Statistics online and online users
2) Loading initialization information at system startup
3) Statistics website Visit volume
4) Integration with spring
5) easy to automate management
6) can activate the number of passivation and log, easy to shut down and restart the server.
The first example of a Web listener is explained steps to create a Web listener
1) Create a class that implements the Listener interface
2) Use annotations or configure the Web. xml file
The first instance of the Web listener
Firstlistener.java Source code:
Package Com.listener;import Javax.servlet.servletcontextevent;import Javax.servlet.servletcontextlistener;import javax.servlet.annotation.weblistener;/** * Application Lifecycle Listener Implementation class Firstlistener * */@ Weblistener ("This was first Listener") public class Firstlistener implements Servletcontextlistener {/** * Default C Onstructor. */Public Firstlistener () {}/** * @see servletcontextlistener#contextinitialized (servletcontextevent) */public void contextinitialized (Ser Vletcontextevent servletcontextevent) {System.out.println ("contextinitialized");}/** * @see servletcontextlistener#contextdestroyed (servletcontextevent) */public void contextdestroyed (Servlet Contextevent servletcontextevent) {System.out.println ("contextdestroyed"); }}
Operation Result:
We compile a JSP page and then launch the Tomcat server, and access to this JSP page will result in the following.
Boot order of the listener
Annotations do not achieve this effect in the order in which the listener is registered in the Web. XML configuration file.
The starting order for the three component Servlets, filter, and listener in the Web project is:
Classification of listeners
(1) According to the interception of the object Division
1) event listener for listening to application environment objects (ServletContext)
2) event listener for listening to user session objects (HttpSession)
3) event listener for listening for request information Object (ServletRequest)
(2) According to the interception of events divided
1) Listen for event listeners that are created and destroyed by the domain object itself
2) listener for adding and deleting properties in the domain object
3) Listen for event listeners that are bound to the state of an object in the HttpSession domain
eight Big Web listener details
We explain it in detail according to the second classification.
Servletcontextlistener Instances
Myservletcontextlistener.java Source code:
Package Com.listener;import Javax.servlet.servletcontextevent;import Javax.servlet.servletcontextlistener;import javax.servlet.annotation.weblistener;/** * Application Lifecycle Listener Implementation class Firstlistener * */@ Weblistenerpublic class Myservletcontextlistener implements Servletcontextlistener {/** * Default constructor. */Public Myservletcontextlistener () {}/** * @see servletcontextlistener#contextinitialized (servletcontextevent) */public void contextinitialized (Ser Vletcontextevent servletcontextevent) {System.out.println ("contextinitialized");}/** * @see servletcontextlistener#contextdestroyed (servletcontextevent) */public void contextdestroyed (Servlet Contextevent servletcontextevent) {System.out.println ("contextdestroyed"); }}
Operation Result:
Httpsessionlistener Instances
Myhttpsessionlistener.java Source code:
Package Com.listener;import Javax.servlet.annotation.weblistener;import javax.servlet.http.HttpSessionEvent; Import javax.servlet.http.httpsessionlistener;/** * Application Lifecycle Listener Implementation class Myhttpsessionlistener * */@WebListenerpublic class Myhttpsessionlistener implements Httpsessionlistener {/** * Def Ault constructor. */Public Myhttpsessionlistener () {}/** * @see httpsessionlistener#sessioncreated (httpsessionevent) */public void sessioncreated (httpsessionevent Httpsessionevent) {System.out.println ("sessioncreated"); }/** * @see httpsessionlistener#sessiondestroyed (httpsessionevent) */public void sessiondestroyed (Httpsessionev Ent httpsessionevent) {System.out.println ("sessiondestroyed"); }}
Operation Result:
Servletrequestlistener Instances
Myservletrequest.java Source code:
Package Com.listener;import Javax.servlet.servletrequestevent;import Javax.servlet.servletrequestlistener;import javax.servlet.annotation.weblistener;/** * Application Lifecycle Listener Implementation class Myservletrequestlistener * */@WebListenerpublic class Myservletrequestlistener implements Servletrequestlistener {/** * Default constructor. */Public Myservletrequestlistener () {}/** * @see servletrequestlistener#requestdestroyed (servletrequestevent) */public void requestdestroyed (Servlet Requestevent servletrequestevent) {System.out.println ("requestdestroyed"); }/** * @see servletrequestlistener#requestinitialized (servletrequestevent) */public void requestinitialized (Ser Vletrequestevent servletrequestevent) {System.out.println ("requestinitialized"); }}
Operation Result:
The JSP pages used by these listeners are:
Java Web Learning (PNS): Listener Learning (I.)