Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.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 web应用程序中在加载上下文时自动运行servlet/操作(tomcat启动)_Java_Jsp_Servlets_Struts2 - Fatal编程技术网

如何在java web应用程序中在加载上下文时自动运行servlet/操作(tomcat启动)

如何在java web应用程序中在加载上下文时自动运行servlet/操作(tomcat启动),java,jsp,servlets,struts2,Java,Jsp,Servlets,Struts2,我想在我的web应用程序部署时执行一些代码 如果我重新启动tomcat,也会发生同样的情况 在整个web应用程序的生命周期中,这应该只执行一次 是否可以在web.xml中设置一些东西,比如启动条目,每次部署web应用程序或重新启动tomcat时都会执行这些东西 请提供帮助。您需要实现ServletContextListner接口 ServletContextListner在javax.servlet包中 下面是一个关于如何做的简短代码 public class MyServletContextL

我想在我的web应用程序部署时执行一些代码

如果我重新启动tomcat,也会发生同样的情况

在整个web应用程序的生命周期中,这应该只执行一次

是否可以在web.xml中设置一些东西,比如启动条目,每次部署web应用程序或重新启动tomcat时都会执行这些东西


请提供帮助。

您需要实现
ServletContextListner
接口

ServletContextListner
javax.servlet
包中

下面是一个关于如何做的简短代码

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
接口

ServletContextListner
javax.servlet
包中

下面是一个关于如何做的简短代码

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