Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can some code run when we first deploy a WAR file?

Is there any method or API that I can use so that whenever I deploy a new WAR file, a part of code should execute or perhaps when Tomcat starts, the respective servlet should start or run some code continuously.

like image 766
ayyappa Avatar asked Mar 17 '11 07:03

ayyappa


People also ask

What happens when you deploy a WAR file?

Java web applications are usually packaged as WAR files for deployment. These files can be created on the command line or with an IDE, like Eclipse. After deploying the WAR file, Tomcat unpacks it and stores all the project files from the webapps directory in a new directory named after the project.

Is a WAR file executable?

5) a WAR file can be runnable/executable (though this is more common with JAR files). Even if it is not, it is almost never used as a "library" for other programs (which is common with JAR files).

When deploying your web application as an external WAR file what are the first two details you need to specify in your app?

At minimum, the <servlet> declaration requires two pieces of information: a <servlet-name> , which serves as a handle to reference the servlet elsewhere in the web. xml file, and the <servlet-class> tag, which specifies the Java class name of the servlet.


1 Answers

Reviving an old question since the only answer doesn't show any example.

In order to run a custom piece of code whenever a web application WAR is deployed/undeployed or Tomcat is started/stopped, you need to:

  1. Implement the ServletContextListener listener and its methods contextInitialized() and contextDestroyed().
  2. Let Tomcat know about your implementation. According to the documentation, you can either add the implementing class to the deployment descriptor, annotate it with WebListener, or register it via one of the addListener() methods defined on ServletContext.

Here is an example (based on this post):

package com.example;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener {
    /** The servlet context with which we are associated. */
    private ServletContext context = null;

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        log("Context destroyed");
        this.context = null;
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        this.context = event.getServletContext();
        log("Context initialized");
    }

    private void log(String message) {
        if (context != null) {
            context.log("MyServletContextListener: " + message);
        } else {
            System.out.println("MyServletContextListener: " + message);
        }
    }
}

And add the following to web.xml (or alternatively, use the WebListener annotation or addListener() method):

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    ...
    <listener>
        <listener-class>com.example.MyServletContextListener</listener-class>
    </listener>
</web-app>
like image 186
Mifeet Avatar answered Oct 25 '22 00:10

Mifeet