| Author |
static variable in a servlet
|
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
|
|
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
Joined: May 15, 2006
Posts: 28
|
|
|
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
Joined: May 24, 2003
Posts: 425
|
|
|
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
Joined: Dec 11, 2004
Posts: 13410
|
|
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.
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
|
|
Thanks All. Nice answers.
|
 |
steve souza
Ranch Hand
Joined: Jun 26, 2002
Posts: 852
|
|
|
Also even with one servlet instance running a static variable is fine if it is thread safe.
|
http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56183
|
|
|
I use static variables in servlets all the time -- mostly for "constants".
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
 |
|
|
subject: static variable in a servlet
|
|
|