在服务器启动阶段怎么创建Servlet对象呢?load-on-startup

      一个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>

希望对你有帮助,祝你有一个好心情,加油!

若有错误、不全、可优化的点,欢迎纠正与补充!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值