开发者

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

开发者 https://www.devze.com 2023-03-04 00:15 出处:网络
This question already has an answer here: Using special auto start servlet to initialize on startup and share application data
This question already has an answer here: Using special auto start servlet to initialize on startup and share application data (1 answer) Closed 7 years ago.

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.


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>
0

精彩评论

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

关注公众号