在前面一篇文章 “【新手记录】servlet的初始 化init方法什么时候被调用? ”中用实例验证了下servlet的init方法何时被调用,虽然有了测试结果。
不过还是感觉不放心,于是到sun官方网站看了下servlet的specification。
得到了肯定的答复:
首先是J2EE api里对servlet的init方法的说明:
init void init(ServletConfig config) throws ServletException Called by the servlet container to indicate to a servlet that the servlet is being placed into service. The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests. The servlet container cannot place the servlet into service if the init method Throws a ServletException Does not return within a time period defined by the Web server
然后又看了下Java™ Servlet Specification Version 2.5 MR6的详细说明
The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these 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-start-up value
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.
如果是负数,或者没有标识load-on-startup参数,这个就有容器自己去选择是否加载了。如果是正数或者0,
那么容器在应用部署的时候就必须要初始化这个servlet。
好吧,不写或者为负数还是有意外的啊!看来还是找specification查东西靠谱。