0

I have a web application in Java (Netbeans). And I have a function that should be called exactly while running the web application, without putting it into the static method main.

I really don't have any idea about how to do.

Thank you in advance.

loulou
  • 331
  • 1
  • 5
  • 20
  • Web applications dont have a main method. Are you trying to run a scheduled job? – Perception Feb 21 '13 at 00:48
  • yes, I succeeded to make a scheduled job thanks to quartz. But my problem is that I don't know how to run a function exactly while deploying my application. Thanks – loulou Feb 21 '13 at 01:04
  • See here: http://stackoverflow.com/a/6016232/680925. – Perception Feb 21 '13 at 01:07
  • One of the answers on the link u gave me is similar to this one http://stackoverflow.com/questions/8184835/call-method-on-undeploy-from-a-java-web-application but it didn't work for me. So i'm going to try the other solutions and let you know. Thanks – loulou Feb 21 '13 at 01:15

1 Answers1

1

Create a class that implements ServletConextListener :

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

public class ListenToMeFirst implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // Run me First while deploying!!!

    }

}

Don't forget to put it in your web.xml file :

<listener>
  <listener-class>path.to.yourListenerClass.ListenToMeFirst</listener-class>
</listener>
loulou
  • 331
  • 1
  • 5
  • 20