Listener: The listener is a Java program that listens to another Java object change (method call, property change)
Realize:
Write a class to implement the interface of the response
Registered listener leverages Web. xml
8 listeners, divided into 3 types
I. Listeners used to monitor the creation and destruction of the three scopes (except the page domain) 1.ServletContextListener listeners to listen for ServletContext object creation and destruction
Create: The server starts and the Web app is loaded immediately after the ServletContext object that represents the current web app is created
Destroy: When the server shuts down or the Web app is removed from the container, it is destroyed as the Web app is destroyed
A. Implementing an interface
PackageCom.itheima.listener;ImportJavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener;/** * Used to monitor servletccontext create and Destroy * @author Fang * */ Public class MyListener implements Servletcontextlistener { @Override Public void contextdestroyed(Servletcontextevent SCE) {System.out.println ("ServletContext was destroyed."+sce.getservletcontext ()); }@Override Public void contextinitialized(Servletcontextevent SCE) {System.out.println ("ServletContext was created."+sce.getservletcontext (). Getcontextpath ()); }}
B. Registering in the Web. XML configuration
<listener> <listener-class>com.itheima.listener.MyListener</listener-class></listener>
2.HttpSessionListener listener to listen for httpsession object creation and destruction
Create: Creates a Session object representing the current conversation the first time the Request.getsession method is called
Destroyed: More than 30 minutes no one is destroyed (can be modified in Web. XML through the session-timeout tag)/Call Invalidate method suicide/The server is not properly shut down with the destruction of the website application, if the server is normal shutdown will be passivated up.
When the server shuts down, the surviving session is stored as a file in Tomcat's work directory as the server shuts down, a process called session passivation
When the server is turned on again, the server will find the session object that was saved before the Sessions.ser file was restored, which is called session activation.
An object that wants to be deactivated with a session (typically a bean) its class must implement the Serializable interface
3.ServletRequestListener Monitoring for ServletRequest object creation and destruction
Create: Request to start creating Request object on behalf of
Destroy: Destroy on behalf of requested request object at end of request
Two. Listener that listens for property changes in three scopes
Servletcontextattributelistener
Public class Myscattrlistener implements Servletcontextattributelistener { Public void attributeadded(Servletcontextattributeevent scab) {System.out.println ("attribute is added to the SC domain"+scab.getname () +":"+scab.getvalue ()); } Public void attributeremoved(Servletcontextattributeevent scab) {System.out.println ("attribute is removed from the SC domain"+scab.getname () +":"+scab.getvalue ()); } Public void attributereplaced(Servletcontextattributeevent scab) {System.out.println ("property is replaced in the SC domain"+scab.getname () +":"+scab.getvalue () +":"+scab.getservletcontext (). getattribute (Scab.getname ())); }//attributes are added to the SC domain age:23//attributes are replaced in the SC domain age:23:24//attribute is removed out of the SC domain age:24}
Where the code core in Setvlet is:
this.getServletContext().setAttribute("age"23); this.getServletContext().setAttribute("age"24); this.getServletContext().removeAttribute("age");
The following two methods are similar to the above
Httpsessionattributelistener
Servletrequestattributelistener
Three. Make JavaBean yourself aware of the listener's state changes in the session, these two listeners are very special, do not need to write their own class implementation also
no need to register in Web. XML, as long as the JavaBean implementation of this interface can work
JavaBean has the following four state changes in the session, two listeners
1.HttpSessionBindingListener
JavaBean is bound to the session
Sessiondidactive (Httpsessionbindingevent event)
JavaBean is removed from the session
Valueunbound (Httpsessionbindingevent Event) method
2.HttpSessionActivationListener
JavaBean as the session is passivated
Sessionwillpassivate (Httpsessionbindingevent event)
JavaBean as the session is activated
Sessiondidactive (Httpsessionbindingevent event)
Re-learn Javaweb---java EE listener