Generally after deploying an application servlet is loaded on first request, so it takes more time to process the first request. In order to overcome it we can use load-on-startup element inside deployment descriptor (web.xml). If you specify the load-on-startup element in web.xml, servlet will be loaded at server start up time and thus it will take less time for processing first request.
Let's see how to configure this element with an example.
web.xml
Let's see how to configure this element with an example.
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>MyServlet1</servlet-name> <servlet-class>com.j2eekart.servlet.Servlet1</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
<servlet> <servlet-name>MyServlet2</servlet-name> <servlet-class>com.j2eekart.servlet.Servlet2</servlet-class> <load-on-startup>2</load-on-startup> </servlet> </web-app>As shown load-on-startup element holds an integer value and:
- If the value if 0 or negative then servlet will be loaded on request time i.e., on first request only.
- If the value is greater than 0 then servlet will be loaded on server start up and calls to init() method of servlet by web container.
- If there are multiple servlets defined with load-on-startup value then they will be loaded in the order declared inside web.xml.
- If there are more than one servlet defined with same load-on-startup value then they will be loaded in the order declared inside web.xml.