gpt4 book ai didi

java - 启动tomcat后执行一些东西

转载 作者:行者123 更新时间:2023-11-28 22:44:18 25 4
gpt4 key购买 nike

我在 apache tomcat 服务器中部署了 Web 应用程序,我需要在部署主 Web 应用程序后启动另一个控制台应用程序(套接字服务器)。这个套接字服务器与主应用程序在同一个 WAR 文件中,并且可以访问 Web 应用程序的所有 beans 和类。
我需要在使用已部署的 web 应用程序启动 tomcat 之后启动它(而不是在打开应用程序的索引页面或其他东西之后)
我该怎么做?

最佳答案

需要实现ServletContextListner接口(interface)

public class MyServletContextListener implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent arg0) {
//Notification that the servlet context is about to be shut down.
}

@Override
public void contextInitialized(ServletContextEvent arg0) {
// do all the tasks that you need to perform just after the server starts

//Notification that the web application initialization process is starting
}

}

并在你的部署描述符web.xml中配置它

<listener>
<listener-class>
mypackage.MyServletContextListener
</listener-class>
</listener>

关于java - 启动tomcat后执行一些东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13303921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com