问题
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