Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 733
Bitmap Avatar asked Feb 22 '26 01:02

Bitmap


1 Answers

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.

like image 184
axtavt Avatar answered Feb 23 '26 15:02

axtavt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!