Keywords: Servlet | load-on-startup | Web Container | JSR 340 | Startup Order
Abstract: This article provides an in-depth analysis of the <load-on-startup> element in Servlet specifications, detailing how integer values affect servlet loading timing and sequence. By examining JSR 340 requirements and web.xml configuration examples, it explains the semantics of positive, zero, and negative values, discusses container implementation differences, and offers best practices. The article also addresses loading order issues with identical load-on-startup values, providing technical guidance for Java Web application deployment.
Overview of Servlet Startup Loading Mechanism
In Java Web application development, the initialization timing of servlets significantly impacts application performance. According to JSR 340 specification, the <load-on-startup> element controls the loading behavior of servlets during Web application startup. The value of this element directly influences the instantiation order and initialization timing of servlets.
Semantic Analysis of load-on-startup Values
The <load-on-startup> element accepts integer values as parameters, with different value ranges having distinct semantic definitions:
When the value is a positive integer or 0, the container must load and initialize the servlet when the application is deployed. The specification requires that containers guarantee servlets marked with lower integers are loaded before those with higher integers. For example:
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>com.foo.framework.axis2.http.FrameworkServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
This configuration indicates that the servlet will be loaded immediately at application startup with the highest priority.
When the value is a negative integer or the element is absent, the container is free to choose the servlet loading timing, typically employing lazy loading upon first request.
Specific Implementation of Loading Order
Containers must strictly adhere to the loading order determined by numerical values. Servlets with smaller values are loaded first, followed by those with larger values. For servlets with identical load-on-startup values, the specification allows containers to determine the loading order autonomously.
Referencing actual deployment scenarios:
<servlet>
<servlet-name>DefaultServletOverride</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.xxx.yyy.MyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
These two servlets share the same load-on-startup value, and their loading order may vary depending on container implementation.
Container Implementation Differences
While JSR 340 specification defines basic behavior, different Web containers may exhibit variations in specific implementations. Resin 3.0 documentation explicitly states: "load-on-startup can specify an (optional) integer value. If the value is 0 or greater, it indicates an order for servlets to be loaded, servlets with higher numbers get loaded after servlets with lower numbers."
Developers should consult specific container documentation during actual deployment to ensure configurations align with expected behavior. Some containers may employ specific loading strategies for servlets with identical load-on-startup values, such as loading in the order they appear in the configuration file.
Best Practice Recommendations
To avoid uncertainties in loading order, it is recommended to assign unique load-on-startup values to each servlet that requires startup loading. Critical infrastructure servlets (such as framework core servlets) should be set to lower values (like 0 or 1) to ensure priority initialization. Business servlets can be set to higher values, loading after infrastructure is ready.
When multiple servlets require the same loading priority, consider refactoring or merging functionality to avoid dependencies on uncertain loading orders. If maintaining identical priority for multiple servlets is necessary, ensure their initialization processes are mutually independent.
Conclusion
The <load-on-startup> configuration is a crucial tool for optimizing Web application startup performance. Understanding its value semantics and container behavior differences enables developers to design reasonable servlet loading strategies, enhancing application maintainability and performance.