By adding a listener for application property changes, we can monitor the changes in attributes of our application.
1. Compile a class that implements ServletContextAttributeListener
The code is as follows: |
Copy code |
Package org. Rudiment. Listener; Import javax. servlet. ServletContextAttributeEvent; Import javax. servlet. ServletContextAttributeListener; Public class MyServletContextAttributeListener implements ServletContextAttributeListener { // When a new attribute is added, the container will call this method. @ Override Public void attributeAdded (ServletContextAttributeEvent arg0 ){ System. out. println ("a new attribute is added:" + arg0.getName () + "value:" + arg0.getValue ()); } // The container will call this method when deleting an attribute. @ Override Public void attributeRemoved (ServletContextAttributeEvent arg0 ){ System. out. println ("an attribute is deleted:" + arg0.getName () + "value:" + arg0.getValue ()); } // The container will call this method when updating an attribute. @ Override Public void attributeReplaced (ServletContextAttributeEvent arg0 ){ System. out. println ("modified an attribute:" + arg0.getName () + "value:" + arg0.getValue ()); } } |
2. Configure web. xml
The code is as follows: |
Copy code |
<Listener> <Listener-class> org. Rudiment. Listener. MyServletContextAttributeListener </listener-class> </Listener> |