Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why cant we instance variables in servlets  RSS feed

 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many people says that we should not instance variable in Servlets , it is very Dangerious

Why.................
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure about the reason, but i think that servlet are initialized only once. And after that it processes only request , so only service method is invoked.

Am i correct? if not, give the reason behind this?

Thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is only one instance for a servlet and many user/many threads/many request may access the instance concurrently. i.e, many threads run on the single instance and it is obvious instance variables are shared by concurrent threads, hence unexpected result which use the instance variable
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also Please search here. similar question discussed many time.
 
Tim Moores
Saloon Keeper
Posts: 3511
77
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that shared data (like an instance variable in multi-threaded code like a servlet) is only a problem if it's mutable. If the data never changes, then there's no problem.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!