What is servlet context listener in Java?
javax.servlet All Superinterfaces: java.util.EventListener public interface ServletContextListener extends java.util.EventListener. Interface for receiving notification events about ServletContext lifecycle changes.
When ServletContextListener is called?
ServletContextListener is an interface that gets notified about ServletContext lifecycle changes. It offers two methods. contextInitialized – Is triggered when the web application is starting the initialization. This will be invoked before any of the filters and servlets are initialized.
What are the methods available in servlet context listener interface?
Methods of ServletContextListener interface public void contextInitialized(ServletContextEvent e): is invoked when application is deployed on the server. public void contextDestroyed(ServletContextEvent e): is invoked when application is undeployed from the server.
Which method of servlet context listener is invoked when an servlet application is initialized?
contextInitialized method
When a servlet context is created, the contextInitialized method of your listener class is invoked, which creates the database connection for the servlets in your application to use if this context is for your application.
How does the servletcontextlistener work in Java?
All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized. Receives notification that the ServletContext is about to be shut down. All servlets and filters will have been destroyed before any ServletContextListeners are notified of context destruction.
Which is an example of listener in servlet?
Listener interfaces declare methods to work with a group of similar events, for example we have ServletContext Listener to listen to startup and shutdown event of context. Every method in listener interface takes Event object as input. Event object works as a wrapper to provide specific object to the listeners.
When to shut down servletcontextlistener in Java EE 6?
Receives notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized. Receives notification that the ServletContext is about to be shut down.
How is the servletcontextlistener notified of context initialization?
Receives notification that the web application initialization process is starting. 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