aspose file tools*
The moose likes Servlets and the fly likes static variable in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "static variable in a servlet" Watch "static variable in a servlet" New topic
Author

static variable in a servlet

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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: 3830
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: 60050
    
  65

I use static variables in servlets all the time -- mostly for "constants".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variable in a servlet
 
Similar Threads
Using LogManager
Static Data
Caching in clustered environment
service locator pattern
caching