[an error occurred while processing this directive]

ServletContextAttributeListener

                         

As we know that the javax.servlet.ServletContext interface used to represents a Servlet's view of the web application it belongs to. All the servlets in an application can access the SevletContext. It means that we keep such information in the servlet context which are common to all the servlets. The elements of the ServletContext are stored in web.xml file. Whenever the container gets start it reads the web.xml file. Occurrence of <context-param> tag should appear before the Servlet tags. 

ServletContext is a interface which helps us to communicate with the servlet container. There is only one ServletContext for the entire web application and the components of the web application can share it. The information in the ServletContext will be common to all the components. Remember that each servlet will have its own ServletConfig. The ServetContext is created by the container when the web application is deployed and after that only the context is available to each servlet in the web application.

The listener ServletContextAttributeListener is an interface and extends the java.util.EventListener class. This listener come into existence when this interface receives notifications of changes to the attribute list on the servlet context of a web application. Remember one thing before gets notified by the container, you should make sure that the implementation class is configured in deployment descriptor for the web application. This listener is used when we want to know when a attribute has been added in a context, when a attribute has been removed and when it is replaced by another attribute. We can also say that this attribute is  used when the developer is interested to be notified when the context attribute changes. Now you may be wondering what is an attribute. An attribute is an object set or you can simply say that it is name/value pair where the name refers to a String and a value refers to the Object.

javax.servlet.ServletContextAttributeListener interface has following methods:

  1. attributeAdded(ServletContextAttributeEvent event): It notifies whenever a new  attribute is added to the servlet context.
  2. attributeRemoved(ServletContextAttributeEvent event): It notifies whenever the attribute is removed from the servlet context.
  3. attributeReplaced(ServletContextAttributeEvent event): It notifies whenever the attribute gets replaced on the servlet context.

In the above methods you can see that we have used ServletContextAttributeEvent class as a parameter to the above methods. This class is a event class which is used for notifications when the changes are made to the attributes of ServletContext in an application.

The class ServletContextAttributeEvent has two methods:

  1. getName() : This method returns the name of the attribute that has been changed on the ServletContext.
  2. getValue(): This method will return the value of the attribute that has been added, removed or replaced by other attribute.

                         

[an error occurred while processing this directive]