Is there a way to do something on a J2EE server-start? I'm using Tomcat [duplicate]
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>
精彩评论