`
yznxing
  • 浏览: 367205 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

servlet specification,init方法~~~

    博客分类:
  • java
阅读更多

在前面一篇文章 “【新手记录】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查东西靠谱。

 

 

 

1
2
分享到:
评论
3 楼 sidneyHere 2012-05-09  
非常好
不过,load-on-startup不设置或者为负数,应该是表示容器自行决定初始化时机。
2 楼 yznxing 2010-12-23  
iq527 写道

那看源码会不会更靠谱些 



呵呵,应该是的。 不够习惯先看specification。

从那之后就没去看这个源码了。。。
你看过了吗?正好可以分享下~~~

呵呵~~~
1 楼 iq527 2010-12-23  

那看源码会不会更靠谱些 

相关推荐

Global site tag (gtag.js) - Google Analytics