Methods | Description |
---|---|
void attributeAdded(ServletContextAttributeEvent e) | It gives notification that a new attribute was added to the context. |
void attributeRemoved(ServletContextAttributeEvent e) | It gives notification that an existing attribute was removed from the context. |
void attributeReplaced(ServletContextAttributeEvent e) | It gives notification that an attribute was replaced on the context |
MyAttributeListener.java
package attribute; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; public class MyAttributeListener implements HttpSessionAttributeListener { @Override public void attributeAdded(HttpSessionBindingEvent event) { String attributeName = event.getName(); Object attributeValue = event.getValue(); System.out.println("Attribute added : " + attributeName + " : " + attributeValue); } @Override public void attributeRemoved(HttpSessionBindingEvent event) { String attributeName = event.getName(); Object attributeValue = event.getValue(); System.out.println("Attribute removed : " + attributeName + " : " + attributeValue); } @Override public void attributeReplaced(HttpSessionBindingEvent event) { String attributeName = event.getName(); Object attributeValue = event.getValue(); System.out.println("Attribute replaced : " + attributeName + " : " + attributeValue); } }
web.xml
<web-app > <listener> <listener-class>attribute.MyAttributeListener</listener-class> </listener> </web-app>
The servlet incorporates the ability to track events in Web applications through listeners. This usefulness permits more effective resource management that depends on the event status status.
The listeners are resemble triggers that can be connected to evenets in application server. With listeners client can track application-level, session-level, life-cycle changes, characteristic changes and so on. The executed interfaces are javax.servlet.Listener interface.