views:

23

answers:

2

How can i load a class on at startup in Tomcat ? I saw "load-on-startup" tag for web.xml file, but can i use it and how should I write my class ?

EDIT: ok, but how can i implements this class and xml is it right ?
< servlet-name>??< /servlet-name>
< servlet-class>??< /servlet-class>
< load-on-startup>10< /load-on-startup>

A: 

Just put in web.xml inside <servelet> tag which you want to load on start up following lines:

 <load-on-startup>N</load-on-startup>

where N load order number.

EDIT.

I think second answer of @BalusC is more appropriate for your case.

Artem Barger
+2  A: 

Those are meant to specify the loading order for servlets. However, servlets are more meant to control, preprocess and/or postprocess HTTP requests/responses while you sound like to be more looking for a hook on webapp's startup. In that case, you rather want a ServletContextListener.

public class Config implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {
        // Do your thing during webapp's startup.
    }
    public void contextDestroyed(ServletContextEvent event) {
        // Do your thing during webapp's shutdown.
    }
}

which you register in web.xml as follows:

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>

See also:

BalusC