aspose file tools*
The moose likes Servlets and the fly likes Servlet life : when is it safe to use class var's in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet life : when is it safe to use class var Watch "Servlet life : when is it safe to use class var New topic
Author

Servlet life : when is it safe to use class var's in servlet

Mike kitbag
Greenhorn

Joined: Dec 26, 2008
Posts: 5
I am new to servlets. i will make this brief (don't want to bore you with all of my studying process and stuff)

Question:

Granted the following class:



To my understanding at startup/(first http req) the following servlet will be initialized, and there for all its class variables alive as long as the servlet isnt destroyed, but until WHEN exactly can i trust a servlet isnt destroyed.
meaning when will the count variable be 0 again (in the code abpve)?

if the answer is its up to the container to decide and we cant know (meaning arbitrary). then what are class variables in servlets useful for ?

Thank you
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You can use instance variables in servlets whenever you want to share a variable among all requests on the servlet. For example an instance of the DAO class or so which you assigned during init().

It's indeed up to the web container when a servlet will be destroyed. Normally this will happen during shutdown, but it can also be destroyed when it isn't been used for a relatively long time. That depends on the appserver implementation used.
[ December 26, 2008: Message edited by: Bauke Scholtz ]
Mike kitbag
Greenhorn

Joined: Dec 26, 2008
Posts: 5
Thank you for the quick answer.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

In other words, read-write class variables are fairly useless in a servlet. They can't be used for anything that isn't supposed to be shared across all threads, and for stuff that should be shared, well, they're not good for that either as there could be more than one instance loaded (based on how the web.xml is configured) or could be reloaded based upon container-dependent decisions.

Use application context or session context to store shared information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Or a datastore like a RDBMS.
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
class variables are not same as instance variables. class variables are static variables.

Class variables can be used to share data across all servlet instances.
Instance variables can be used to share data across multiple threads.


krad
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
Correction:
Instance variables can be used to share data across multiple threads operating on the same instance.
 
 
subject: Servlet life : when is it safe to use class var's in servlet