On Mon, 6 Aug 2001, Diu Lee Lo Mo wrote:
> Dear Craig,
>
> Is that the servlet instance will be created once a
> request is coming in ? The target class will call
> init() when it is called at first time ?
>
That is correct. See the servlet spec for more details:
http://java.sun.com/products/servlet/download.html
> My whole picture is :
> request -> target class -> init()
>
Almost, but not quite, complete. The way I think of it is:
request --> servlet definition --> init() if needed --> service()
The differences from your picture:
* Which servlet definition is selected is based on the various
<servlet-mapping> elements in your web.xml file. See the rules
in the servlet spec for how this is determined.
* There will be an instance of a servlet *per-servlet-definition*
(i.e. per <servlet> element in your web.xml file), unless your
servlet implements SingleThreadModel -- in which case the container
might create more than one instance. (Tomcat doesn't do this.)
* The very first time a particular <servlet> definition is accessed,
the instance will be created and init() will be called. Alternatively,
you can declare a <load-on-startup> element inside the <servlet>
definition to cause the servlet to be loaded when the web application
is first started. This is quite handy when the initialization process
is time consuming, because you can make it happen before the first
request to the servlet.
* For each request (including the first), service() will be called.
* Tomcat supports a non-spec-defined (but very common) feature called
the "invoker", where you can execute a URL that includes
"/servlet/{classname}". In essence, Tomcat creates a <servlet>
definition on the fly the first time this is encountered, and the
rest of the rules defined above are applied.
> Thx.
>
> M.T.
>
Craig