Context listener in java

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

Overview Package Class Tree Deprecated Index Help
PREV CLASS NEXT CLASS FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD
Submit a bug or feature

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

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
implements ServletContextListener < public MyContextListener () <
>

public void contextInitialized ( ServletContextEvent servletContextEvent ) < // Get the context
ServletContext servletContext = servletContextEvent.getServletContext () ;

// Set a context attribute
try <
System.out.println ( «[MyContextListener] Application X is starting» ) ;
servletContext.setAttribute ( «foo» , «bar» ) ;
> catch ( Exception e ) <
System.out.println ( «[MyContextListener] Error setting context attribute: » + e.getMessage ()) ;
>
>

public void contextDestroyed ( ServletContextEvent servletContextEvent ) < // Get the context
ServletContext servletContext = servletContextEvent.getServletContext () ;

// Output the context variable we set earlier
System.out.println ( «[MyContextListener] Application X is shutting down» ) ;
System.out.println ( «[MyContextListener] Value of foo is: » + servletContext.getAttribute ( «foo» )) ;

// Clean up (not really necessary as the context is being destroyed, but let’s be neat)
servletContext.removeAttribute ( «foo» ) ;
>
>

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.

Other Listener Examples

Источник

Читайте также:  Byte short int long float double in java
Оцените статью