0

I have a JSP Project, running on Tomcat locally and on Glassfish on the web.

I have to automatically execute a batch (setup timers) as soon as the application is deployed before responding to any request.

Is there a standard way to solve this problem?

Thanks.

Elie
  • 6,915
  • 7
  • 31
  • 35

2 Answers2

2
@WebListener
public class SomeClass implements ServletContextListener{
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //Put code here
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
}
Esben Skov Pedersen
  • 4,437
  • 2
  • 32
  • 46
1

For those that don't know, the annotation in the above example tells the servlet container that this is a listener

@WebListener

If you are configuring using web.xml, you would leave the annotation off of the class and define the listener after filter-mapping, but before servlet in the web.xml file.

<listener>
    <listener-class>yourpackage.SomeClass</listener-class>
</listener>

Either way, the container will run it at startup.

jeffb
  • 360
  • 1
  • 2