Eight Listeners of javaweb

1. Listeners for servletContext, request, and session domains (six listeners)

The steps to use these six listeners are as follows:
1. Create a class to implement the interface to use the listener;
2. Configure in web.xml;

①, ServletContextListener
②, HttpSessionListener
③, ServletRequestListener

These three listeners monitor the opening and closing of the servletContext, request, and session domains. (There are two events that need to be implemented: creation and destruction)
④, ServletContextAttributeListener
⑤, HttpSessionAttributeListener
⑥, ServletRequestAttributeListener

These three are listeners that monitor the modification, addition, and deletion of objects in the three domains of servletContext, request, and session (inside To achieve three events (modification, addition, and deletion).

2. Medium-aware listeners for session domain (two)

There is no need to configure in web.xml when using these two listeners.

①, HttpSessionBindingListener
This is to first create a bean object class so that the object class implements the binding and unbinding events in the HttpSessionBindingListener interface. The binding event of the HttpSessionBindingListener will run whenever the object is stored in the session. The unbind event will run when the object is removed.
②, HttpSessionActivationListener
also needs to first create a bean object class to enable the object class to implement the passivation and activation events in the HttpSessionActivationListener interface. When implementing this interface, it must also implement Serializable serialization interface, so that it can be activated after passivation . (Passivation: Move the objects that already exist in the session from the session memory to the disk, and activate: Move the objects in the session on the disk to the memory. Passivation and activation can be applied to website optimization. You can passivate the user's session object when you log in to the website and do not operate. If you want to set the passivation time operation yourself, you need to create a new context.xml file under the project’s META-INF and configure it here. as follows:

<Context>
 <!-- maxIdleSwap:session中的对象多长时间不使用就钝化 -->
 <!-- directory:钝化后的对象的文件写到磁盘的哪个目录下  配置钝化的对象文件在                                             work/catalina/localhost/钝化文件 -->
 <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
  <Store className="org.apache.catalina.session.FileStore" directory="填上自己的文件名" />
 </Manager>
</Context>

Guess you like

Origin blog.csdn.net/weixin_44061648/article/details/100816377