54

I need to remove temp files on Tomcat startup, the pass to a folder which contains temp files is in applicationContext.xml.

Is there a way to run a method/class only on Tomcat startup?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
dcave555
  • 904
  • 1
  • 9
  • 8
  • UPDATE: I had a temporary malfunction - you should use a [ContextListener](http://www.docjar.com/docs/api/listeners/ContextListener.html), not a `SessionListener`. – Hank Gay Oct 01 '08 at 15:59

3 Answers3

81

You could write a ServletContextListener which calls your method from the contextInitialized() method. You attach the listener to your webapp in web.xml, e.g.

<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 on webapp startup, rather than Tomcat startup, but that may amount to the same thing.

bluish
  • 26,356
  • 27
  • 122
  • 180
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • This is also portable across Java servlet containers. The best way in my opinion. Other options are listed here- http://blog.eisele.net/2010/12/seven-ways-to-get-things-started-java.html – Michael K Jun 18 '14 at 15:25
  • @skaffman but during the tomcat server in eclipse starts contextInitialized() function is calling but MyClass.INSTANCE is not calling.MyClass is a enum class and it creating INSTANCE once.Tomcat fails to start. the code is `public void contextInitialized(ServletContextEvent contextEvent) { MongoDBClass.INSTANCE.getSomeDB().getCollection("UserDB"); context = contextEvent.getServletContext(); MongoDBClass.INSTANCE.getSomeDB().getCollection("UserDB"); }` – Vicky May 06 '15 at 06:57
15

You can also use (starting Servlet v3) an annotated aproach (no need to add anything to web.xml):

   @WebListener
    public class InitializeListner implements ServletContextListener {

        @Override
        public final void contextInitialized(final ServletContextEvent sce) {

        }

        @Override
        public final void contextDestroyed(final ServletContextEvent sce) {

        }
    }
xav
  • 5,452
  • 7
  • 48
  • 57
Alexander Drobyshevsky
  • 3,907
  • 2
  • 20
  • 17
  • Can i add http server starting code within contextInitialized. Because after adding this am messed up with exception and errors – Hema Apr 07 '17 at 11:54
3

I'm sure there must be a better way to do it as part of the container's lifecycle (edit: Hank has the answer - I was wondering why he was suggesting a SessonListener before I answered), but you could create a Servlet which has no other purpose than to perform one-time actions when the server is started:

<servlet>
  <description>Does stuff on container startup</description>
  <display-name>StartupServlet</display-name>
  <servlet-name>StartupServlet</servlet-name>
  <servlet-class>com.foo.bar.servlets.StartupServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet> 
Jonny Buchanan
  • 61,926
  • 17
  • 143
  • 150
  • 3
    Before Servlet 2.4 (or was it 2.3?), that's what people did. But with the addition of context listeners, this is no longer necessary. – skaffman Oct 01 '08 at 20:56
  • That's good tp know - a legacy application we're "refactoring" (it's not a rewrite from the ground up with a better framework and requirements changing all over the place, honest!) at the moment to run on a 2.4 container is still using this technique. – Jonny Buchanan Oct 02 '08 at 08:25