> A global variable like your 'agentArray' is associated with the
> instance of the servlet not the request. You said in your previous
[quoted text clipped - 10 lines]
>
> Amit :-)
Yep got it, that was my clumsy way of saying that I now understand that
class variables are not instantiated once per call to the servlet :)
> A global variable like your 'agentArray' is associated with the
> instance of the servlet not the request.
It's not directly a global variable in the sense of the Java language.
It's simply an instance variable (or member).
> You said in your previous
> message that it is instantiated every time the servlet is entered but
> the fact is that the servlet is entered in doXXXX methods rather than
> in constructor because server does not (or might not) create one
> servlet instance per request.
I guess, the container is actually allowed to create a servlet instance
per request - although that would be inefficient.
> Actually many requests are served by
> same instance of servlet and this is how J2EE works.
Right.
> One more thing
> is that the class variables in servlets are not container variables
> by any means but they are class variables only.
It's not a class variable, it's an instance variable!
> The only thing you
> need to understand is that one request for the servlet is not
> necessarily going to create an instance of the servlet. This is true
> of course if you are not implementing SingleThreadModel interface.
Even then. The container is only required to ensure that no more than one
thread at a time enters one of the doX methods of a single thread model
servlet. Subsequent requests may be routed through the same servlet
nevertheless.
Use instance variables in servlets only for things that should have the
same life cycle as the servlet instance. Everything else must be done
with local variables. IMHO servlets should not be complex anyway so if
you need more complex calculations you can for example resort to command
pattern, i.e., delegate the real work to another class's instance that is
created per request.
Folks, know your tools! The servlet spec isn't really unreadable. Just
spend the time to read and understand. Will save you a lot of hassle...
Kind regards
robert
Pep - 23 Nov 2005 15:45 GMT
<snip>
> Folks, know your tools! The servlet spec isn't really unreadable. Just
> spend the time to read and understand. Will save you a lot of hassle...
>
> Kind regards
>
> robert
Well I will certainly be doing more reading on this littel puppy now that I
fell foul of that :)
Viator - 23 Nov 2005 15:45 GMT
Thanks for correcting me on SingleThreadModel.
Regarding class variables; they are class variable only while instance
variables are instance variable.
I admit that I should have written instance variable there.
Thanks again.
Amit :-)