开发者

How to automatically execute some code every time my JSP Application is deployed [duplicate]

This question already has an answer here: 开发者_如何学PythonUsing special auto start servlet to initialize on startup and share application data (1 answer) Closed 7 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜