user2641906
user2641906

Reputation: 93

How to specify LifeCycle Listener listener class in web.xml?

I want to get notified once the server got started successfully.. For that I have added the below in the web.xml

<listener>    <listener-class>com.server.container.Listeners</listener-class> </listener>

Listeners is class which implements org.apache.catalina.LifecycleListener.

Is this correct? As of now i am not getting any notification during server startup end. Do i need to do anything extra?

Upvotes: 1

Views: 1516

Answers (1)

Vasu
Vasu

Reputation: 22402

In J2EE, Listener notifies whenever some action (context created, destroyed, request or session attribute added, removed, etc..) happens on the server.

Please find the below sample listener code below:

ApplicationListener Class (in your project):-

package com.myproject;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ApplicationListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println(" Server Starting !!!!!! ");

        //Any other code you can place here
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println(" Server Shutting down !!!!!! ");
    }
}

web.xml changes: Add below code to your web.xml

<listener>
        <listener-class>
             com.myproject.ApplicationListener 
        </listener-class>
   </listener>

Also, please ensure that you have got "servlet-api.jar" file in your classpath.

Upvotes: 1

Related Questions