Saturday, 5 September 2015

How to make sure a servlet is loaded at the application startup?

Usually servlet container loads a servlet on the first client request but sometimes when the servlet is heavy and takes time to loads, we might want to load it on application startup. We can use load-on-startup element with servlet configuration in web.xml file or use WebServlet annotation loadOnStartup variable to tell container to load the servlet on system startup.
<servlet>
    <servlet-name>foo</servlet-name>
    <servlet-class>com.foo.servlets.Foo</servlet-class>
    <load-on-startup>5</load-on-startup>
</servlet>
The load-on-startup value should be int, if it’s 0 or negative integer then servlet container will load the servlet based on client requests and requirement but if it’s positive, then container will load it on application startup.
If there are multiple servlets with load-on-startup value as 1,2,3 then lower integer value servlet will be loaded first.

No comments:

Post a Comment