static variables in servlets

S

Suresh

Hi,

Does static variables in servlets make any sense. As far as I understand, a
servlet object is created only once (correct me if I am wrong). But it may
be called from many threads. So declaring a variable as static should be the
same as declaring it as a ordinary variable.

Could someone give me some pointers where I can find more info ?



Thanks.
 
G

GaryM

Does static variables in servlets make any sense. As far as I
understand, a servlet object is created only once (correct me if I
am wrong). But it may be called from many threads. So declaring a
variable as static should be the same as declaring it as a
ordinary variable.

Could someone give me some pointers where I can find more info ?

This is true, but as far as I know each registered name for a servlet
is associated with one instance of the servlet. So there could
potentially be more than one instance of that servlet extant. In this
case class variables are the way to go.
 
M

Marko Lahma

Does static variables in servlets make any sense. As far as I understand, a
servlet object is created only once (correct me if I am wrong). But it may
be called from many threads. So declaring a variable as static should be the
same as declaring it as a ordinary variable.

There can be more than one servlet instance of given class running
inside the servlet engine. So static variables make sense in that way.
There are of course concurrency issues and synchonization is needed
because of this. If you want to have application wide shared data use
ServletContext.

-Marko
 
J

John C. Bollinger

Suresh said:
Hi,

Does static variables in servlets make any sense. As far as I understand, a
servlet object is created only once (correct me if I am wrong). But it may
be called from many threads. So declaring a variable as static should be the
same as declaring it as a ordinary variable.

A servlet class may be instantiated more than once by a servlet
container, under the following conditions:

(1) The servlet implements SingleThreadedModel

or

(2) The container releases one instance and then later instantiates
another. The container does not have to use any instance for any
particular amount of time.

Distributed servlet containers add more possibilities, but they're not
really relevant here.

In any case, a static variable is _not_ functionally equivalent to an
instance variable in a servlet. I recommend you do not use mutable
static variables in your servlet; if you need to store persistent state
then use context or session attributes, as appropriate.

Actually, I also recommend that you not use mutable instance variables
in your servlets. Mutable static and instance variables are invitations
to thread safety problems. That particular problem goes away for
instance variables if you implement SingleThreadedModel, but is always
there for static variables. SingleThreadedModel can hamper performance.
Could someone give me some pointers where I can find more info ?

Read the Servlet API, available free from Sun. I don't have the
specific URL handy, but start at http://java.sun.com.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top