一个Servlet类型,服务器只创建一个实例对象。服务器会在Servlet第一次被访问时创建Servlet(默认),或者是在服务器启动时创建Servlet。
如果想让服务器启动时就创建Servlet,需要在web.xml文件中配置,在<servlet>元素中配置< load-on-startup >元素可以让服务器在启动时就创建该Servlet。
The element load-on-startup indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the Web application. The element content of this element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-startup value.
< load-on-startup >元素值为整数:
元素值小于0时 -> 该配置无线,访问时创建servlet
元素值大于等于0时 -> 优先初始化 0 级别,数字越大初始化越靠后,也就是数字小的优先初始化
元素值有相同值时 -> 优先按数字从小到大初始化,如果值相等的话,容器选择自己的排序策略去加载
注:当为非整数或不填值时,该servlet不能正常初始化
<servlet>
<servlet-name>welcomeServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>welcomeServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
希望对你有帮助,祝你有一个好心情,加油!
若有错误、不全、可优化的点,欢迎纠正与补充!