Java – is there a way to run methods / classes only when Tomcat / wildfly / GlassFish starts?

I need to delete the temporary files during Tomcat startup and transfer them to the folder containing the temporary files, which is located in ApplicationContext XML

Is there a way to run methods / classes only when Tomcat starts?

Solution

You can write a servletcontextlistener that calls your method from the contextinitialized () method You attach listeners to the web Webapp in XML, for example

<listener>
   <listener-class>my.Listener</listener-class>
</listener>

and

package my;

public class Listener implements javax.servlet.servletcontextlistener {

   public void contextInitialized(ServletContext context) {
      MyOtherClass.callMe();
   }
}

Strictly speaking, this is only run once when webapp starts, not Tomcat starts, but this may be the same

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>