Is there a way to do something on a J2EE server-start? I'm using Tomcat [duplicate]

岁酱吖の 提交于 2019-12-22 10:48:08

问题


I'm trying to get a J2EE server to register (read: send some message to) with another server on its own initiative - not as a response to something. Surprisingly, I've found very little information or questions on whether there are events and/or classes to extend that will give me a handle on "server-start". I could always write a script that first deploys to server, then prompts it with a request, but I'd really rather have a cleaner solution..

Thanks.


回答1:


Implement ServletContextListener and do the job in contextInitialized() method.

public class Config implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // Do stuff during server startup.
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // Do stuff during server shutdown.
    }

}

When you're using Tomcat 7, register it as follows to get it to run

@WebListener
public class Config implements ServletContextListener {

Or when using Tomcat 6 or older, register it in web.xml instead

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>


来源:https://stackoverflow.com/questions/5951294/is-there-a-way-to-do-something-on-a-j2ee-server-start-im-using-tomcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!