Wednesday, May 31, 2006

Methods of ServletContextListener

public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServletContextEvent sce);

Utilizing the contextInitialized method is similar to writing code in a servlet's init( ) method. However, using application events make the codes available throughout the whole application, not only from inside a servlet.
Both methods of the ServletContextListener interface pass a ServletContextEvent class. This class has the following signature:

public class ServletContextEvent extends java.util.EventObject

The ServletContextEvent has only one method: getServletContext.

This method returns the ServletContext that is changed.Note that before you can set a ServletContext attribute, you first need to obtain the ServletContext object using the getServletContext method of the ServletContextEvent class.

ServletContext servletContext = sce.getServletContext();

Listening to ServletContextAttributeListener :-

public void attributeAdded(ServletContextAttributeEvent scae)
public void attributeRemoved(ServletContextAttributeEvent scae)
public void attributeReplaced(ServletContextAttributeEvent scae)

The attributeAdded method is called when a new attribute is added to the ServletContext object. The attributeRemove method is called when an attribute is removed from the ServletContext object, and the attributeReplaced method is invoked when an attribute in the ServletContext object is replaced.Note that the attributeReplaced also is called when the attributeAdded is triggered.

0 Comments:

Post a Comment

<< Home