Home > Java > how to configure a startup class for a web application in Tomcat

how to configure a startup class for a web application in Tomcat

In this tutorial you will learn how to configure a startup class for a web application in Tomcat 5.5.x or Tomcat 6.

Step 1:Let us assume Tomcat is installed in C:\tomcat. Let us assume you want to define a startup class for a web application named reminder. Navigate to C:\tomcat\webapps\reminder\WEB-INF and open the file web.xml. Configure the startup class ApplicationStartUpListener under the package com.corelabs as in the screen shot below.

<web-app>
          <listener>
            <listener-class>com.corelabs.ApplicationStartUpListener</listener-class>
        </listener>

 Step 2: The startup class ApplicationStartUpListener should follow certain rules like implementing javax.servlet.ServletContextListener. The Java source of the ApplicationStartUpListener is provided in the below screen shot.

package com.corelabs;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;

/**
 * @author ramb
 */
public class ApplicationStartUpListener implements ServletContextListener {

    private static final Logger log = Logger.getLogger(ApplicationStartUpListener.class);
    private ServletContext context = null;

    public ApplicationStartUpListener() {
    }

    public void contextInitialized(ServletContextEvent event) {
  //Do Something you wish when application is loaded.
    }

    public void contextDestroyed(ServletContextEvent event) {
        this.context = null;
    }
}

Advertisements
Categories: Java
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: