Servlet listeners : ServletContextListener example.

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello, this tutorial will explain ServletContextListener interface in java EE and how to use it.

What is the need of ServletContextListener?

      Sometime we may have a requirement that some code should run before a web application starts. For example, we need to create a database connection so that web application can use it whenever it performs some operations and when application shuts down, we can close database connection.

How can we achieve this?

Java EE specification provides an interface named ServletContextListener which serves our purpose. ServletContextListener listens to the lifecycle events of a servlet context. This interface gets notified whenever an application with which listener is associated starts up and shuts down. Here is what javadoc says about it.

Implementations of this interface receive notifications about changes to the servlet context of the web application they are part of. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.

If you want to listen when web application starts, use contextInitialized(ServletContextEvent event) method.

Notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized.

If you want to listen when web application stops, use contextDestroyed(ServletContextEvent event) method.

Notification that the servlet context is about to be shut down. All servlets and filters have been destroy()ed before any ServletContextListeners are notified of context destruction.

Create a listener as follows


Now we need to configure this listener in  web.xml  as follows

Now we are done with configuration, deploy the app in tomcat server and start tomcat. You will see the log.




Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

4 thoughts on “Servlet listeners : ServletContextListener example.

  • April 4, 2016 at 6:13 pm

    Very clear explanation!! Thank you!

  • May 20, 2016 at 12:56 pm

    well explained simple too

  • May 5, 2017 at 11:23 am

    Hi Prasad,

    I have tried this ServletContextListener with an example. i have tried to create a ServletContext object and set an attribute. And later i have tried to use that attribute in the application, But while starting the server itself, exception has been triggered. but server is getting started.
    Given below the snippet,

    public class MyServletContextListener implements ServletContextListener {

    * Default constructor.
    public MyServletContextListener() {
    // TODO Auto-generated constructor stub

    * @see ServletContextListener#contextInitialized(ServletContextEvent)
    public void contextInitialized(ServletContextEvent arg0) {

    ServletContext sContext = arg0.getServletContext();

    sContext.setAttribute(“port”, “8181”);
    System.out.println(“Port initialized by Servlet context Listner” + sContext.getAttribute(“port1”).toString());


    * @see ServletContextListener#contextDestroyed(ServletContextEvent)
    public void contextDestroyed(ServletContextEvent arg0) {


    What here is wrong.


Leave a Reply

Your email address will not be published. Required fields are marked *