How to automatically execute some code every time my JSP Application is deployed [duplicate]
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.
@WebListener
public class SomeClass implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
//Put code here
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
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.
精彩评论