如何在java web应用程序中在加载上下文时自动运行servlet/操作(tomcat启动)
我想在我的web应用程序部署时执行一些代码 如果我重新启动tomcat,也会发生同样的情况 在整个web应用程序的生命周期中,这应该只执行一次 是否可以在web.xml中设置一些东西,比如启动条目,每次部署web应用程序或重新启动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
请提供帮助。您需要实现
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