The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

helmers (7) [Avatar] Offline
#1
2nd edition, Section 4.5: Servlet Life Cycle

After reading sections 4.5.1 (Loading and instantiating a servlet) and 4.5.2 (Initializing a servlet), I got very confused when I ended up in the subsection ‘Preinitializing a servlet’ of 4.5.2.

P.45 (4.5):
The first step is loading and instantiating the servlet class; the servlet is now considered to be in the loaded state.
==> OK, so a servlet is loaded, if it’s instantiated.

P.45 (4.5.1):
When we startup a servlet container, it looks for a set of configuration files, also called the deployment descriptors … The servlet container creates an instance of the given servlet class … At this time, the servlet is loaded.
==> Perhaps it’s my fault, but I interpreted this as: All servlets defined in the deployment descriptor(s), are loaded when the container starts up.

P.46,47 (4.5.2):
The servlet is initialized after the init() method returns.
==> OK, clear.

P.47 (Preinitializing a servlet):
Usually, a servlet container does not initialize the servlets as soon as it starts up. It initializes a servlet when it receives a request for that servlet for the first time …The servlet specification defines the <load-on-startup> element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up.
==> Here the confusion starts. In 4.5.1, the loaded state was defined as a servlet being instantiated. On the other hand, the load-on-startup deployment descriptor element applies also to initialization! The book does not point out / clarify this ambiguity of the term ‘load’ (which by now has been used in three different ways; two of which on P.45 – see above).
==> “…make the servlet container load and initialize…”: So after finishing section 4.5.2 I suddenly find out that 4.5.1 is not correct? Servlets are not always instantiated during container startup? Does the element load-on-startup only apply to the initialization, or also to the instantiation?!

The more I thought about it the more confused I got, so I decided to look in the servlet specs. Those clarified everything in just a few words.

SRV.2.3.1 Loading and Instantiation
The servlet container is responsible for loading and instantiating servlets. The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request.

SRV.13.3 Deployment Descriptor
…
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. ...
If the value is a negative integer, or the
element is not present, the container is free
to load the servlet whenever it chooses.



Conclusion:
If the load-on-startup element is not present or has a negative value
- the container is free to choose when to load / instantiate / initialize the servlet
If the load-on-startup element is positive or 0
- the servlet is loaded and initialized when the container is started

But I still think the term ‘load’ is very confusing… I think by now I've seen the term being used in 5 different ways...

Message was edited by:
helmers