Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在启动tomcat之后执行某些操作_Java_Tomcat - Fatal编程技术网

Java 在启动tomcat之后执行某些操作

Java 在启动tomcat之后执行某些操作,java,tomcat,Java,Tomcat,我在ApacheTomcat服务器中部署了web应用程序,在部署主web应用程序之后,我需要启动另一个控制台应用程序(套接字服务器)。此套接字服务器与主应用程序位于同一WAR文件中,并且可以访问所有bean和web应用程序类。 我需要在使用部署的web应用程序启动tomcat之后启动它(而不是在打开应用程序的索引页或其他内容之后) 我如何才能做到这一点?使用ServletContextListener,您可以在web.xml中配置它 当web应用启动和停止时,您将获得句柄。使用ServletCo

我在ApacheTomcat服务器中部署了web应用程序,在部署主web应用程序之后,我需要启动另一个控制台应用程序(套接字服务器)。此套接字服务器与主应用程序位于同一WAR文件中,并且可以访问所有bean和web应用程序类。
我需要在使用部署的web应用程序启动tomcat之后启动它(而不是在打开应用程序的索引页或其他内容之后)

我如何才能做到这一点?

使用ServletContextListener,您可以在web.xml中配置它


当web应用启动和停止时,您将获得句柄。

使用ServletContextListener,您可以在web.xml中配置它


您将在web应用程序启动和停止时获得句柄。

您需要实现
ServletContextListner
接口

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>

mypackage.MyServletContextListener

您需要实现
ServletContextListner
接口

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>

mypackage.MyServletContextListener