Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE Enterprise Application: perform some action on deploy/startup [duplicate]

I would like to perform some action as soon as my application (Enterprise Application with Business Logic, EJB, and a Client, Web) is deployed. For example I would like to make some entity in a persistent state, or otherwise create a file. How can I do that?

Thanks.

like image 813
Mauro Avatar asked May 25 '11 07:05

Mauro


1 Answers

Configure SerlvetContextListener and override contextInitilized()

in your web application description , web.xml

<web-app ...>
    <listener>
        <listener-class>com.someCompany.AppNameServletContextListener</listener-class>
    </listener>
</web-app

package com.someCompany;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class AppNameServletContextListener implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("ServletContextListener destroyed");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("ServletContextListener started");   
                // do the things here 
    }
}
like image 158
jmj Avatar answered Oct 14 '22 10:10

jmj