Servlets - SPLessons

Servlet Attribute Listener

Home > Lesson > Chapter 26
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet Attribute Listener

Servlet Attribute Listener

shape Description

Servlet Attribute Listener, Modifying the condition of a particular instance is known as an event. Servlet API(Application Program interface)2.4 onwards the event handling was introduced. There are many event classes and their corresponding listeners where each class will have own functionality, all the classes and listeners will available in javax.servlet, javax.servlet.http packeges. ServletContextAttributeEvent class let's programmer know if an attribute in a web application context has been added, removed or replaced. Implementation of ServletContextAttributeListener interface, receive notifications of changes to the attribute list on the ServletContext of a web application. Click Here To Know More About Attributes

classes and methods

shape Description

On HttpSessionAttribute object Following are the methods of ServletContextAttributeListener.
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

shape Example

Servlet Attribute Listener - Following is the way to write the code by using attribute listener. MyAttributeListener.java [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); } }[/java] web.xml [xml]<web-app > <listener> <listener-class>attribute.MyAttributeListener</listener-class> </listener> </web-app>[/xml] 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.

Summary

shape Key Points

  • Servlet Attribute Listener - While working with events tag should be added in web.xml
  • Servlet Attribute Listener - Through WAR file source code file needs to be deploy in Tomcat.