• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variable in a servlet

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Defining a static variable in a servlet can be useful in any scenerio?

Static variable are shared between instances but servlet has only one instance at a time (except clustered environment).

So I guess, except clustered environment, no body will use static member in servlet.

Is it right?

Thanks.
 
narender kaasam
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A very good question.I agree with your expalnation.Because only one instance will be created in servlet.So there is no use os Static varibales.
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not always some J2EE server allows you to create pool for SingleThreadedServlet. For example in WebLogic you can configure to have more than one instance of the same servlet.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also create multiple servlet instances from the same class by adding new servlet entries in your deployment descriptor.
I this case both servlets (even though they have different names) will share the same static variables.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All.
Nice answers.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also even with one servlet instance running a static variable is fine if it is thread safe.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64962
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use static variables in servlets all the time -- mostly for "constants".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic