Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mike kitbag
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the quick answer.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or a datastore like a RDBMS.
 
kranthi adari
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kranthi adari
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction:
Instance variables can be used to share data across multiple threads operating on the same instance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic