Context listener in java
Interface for receiving notification events about ServletContext lifecycle changes.
In order to receive these notification events, the implementation class must be either declared in the deployment descriptor of the web application, annotated with WebListener , or registered via one of the addListener methods defined on ServletContext .
Implementations of this interface are invoked at their contextInitialized(javax.servlet.ServletContextEvent) method in the order in which they have been declared, and at their contextDestroyed(javax.servlet.ServletContextEvent) method in reverse order.
Since: Servlet 2.3 See Also: ServletContextEvent
Method Summary | |
---|---|
void | contextDestroyed (ServletContextEvent sce) Receives notification that the ServletContext is about to be shut down. |
void | contextInitialized (ServletContextEvent sce) Receives notification that the web application initialization process is starting. |
contextInitialized
All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized.
Parameters: sce — the ServletContextEvent containing the ServletContext that is being initialized
contextDestroyed
All servlets and filters will have been destroyed before any ServletContextListeners are notified of context destruction.
Parameters: sce — the ServletContextEvent containing the ServletContext that is being destroyed
| ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 2009-2011, Oracle Corporation and/or its affiliates. All Rights Reserved. Use is subject to license terms.
Generated on 10-February-2011 12:41
Context listener in java
A context listener receives notifications when the web application (ie: the context) is starting up or shutting down.
Creating a ContextListener
To create a Context Listener, create a class which implements the ServletContextListener interface.
This involves implementation of the following two methods:
public void contextInitialized ( ServletContextEvent servletContextEvent ) ; public void contextDestroyed ( ServletContextEvent servletContextEvent ) ; |
Each listener class must also have a public constructor taking no arguments.
contextInitialized()
This method is called whenever the application starts up.
contextDestroyed()
This method is called whenever the application is shutting down.
ServletContext![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%200%200'%3E%3C/svg%3E)
The ServletContext is passed to both of the above methods in the ServletContextEvent object.
It can be retrieved as follows:
This object has setAttribute(), getAttribute() and removeAttribute() methods which can be used to store context-specific information.
Full Example
import javax.servlet.*; public final class MyContextListener public void contextInitialized ( ServletContextEvent servletContextEvent ) < // Get the context // Set a context attribute public void contextDestroyed ( ServletContextEvent servletContextEvent ) < // Get the context // Output the context variable we set earlier // Clean up (not really necessary as the context is being destroyed, but let’s be neat) |
Adding the Context Listener to web.xml
Add the above class to WEB-INF/classes, and add the following in web.xml to activate our new ContextListener.
Note that the above class was not put in a package, so no package name is specified in the tag.