Bitmap
Bitmap

Reputation: 12538

Startup a Servlet before loading app context

I have a servlet which is bundled as part of my application. I want to be able to configure Tomcat or Spring to load this servlet at boostrap before the application context is finally initialize.

I am doing this because - I am using the servlet to do some initial enviroment checking and file loading before the actual context gets fired.

Do anyone knows a good of doing this?

I have added servlet definition like this;

    <servlet> 
            <servlet-name>init-class</servlet-name>
            <servlet-class>package.initClass</servlet-class>
            <load-on-startup>0</load-on-startup> 
    </servlet>

This only gets fired after the app context has finished loading.

Upvotes: 3

Views: 5641

Answers (2)

axtavt
axtavt

Reputation: 242686

You need to place this initialization logic into ServletContextListener instead of servlet and declare it in web.xml as

<listener><listener-class>YourListenerClass</listener-class></listener>

Also, if your Spring context is loaded by ContextLoaderListener, your listener should be declared in web.xml before it, since listeners are fired in declaration order.

Upvotes: 7

Maurizio Cucchiara
Maurizio Cucchiara

Reputation: 895

Maybe the Listner is what you're looking for

Upvotes: 0

Related Questions