Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static data in Servlets

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am creating a servlet which creates and stores static data. Now the first time UserA calls this servlet and the data is created.
My question is that what will happen when this servlet is called by UserB.
1. Will the same set of data will be shared by all.
2. Or a new set of data will be created for each different user.

Thanks in advance
Sanjana
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

very wild guess but....IMHO,
it will be the same data, different data will not be created..
since static in java is a class property and not
instance based....
am i even close ? i will be checking here.....
- satya
 
Prabu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai sanjana,

Since the data set is static,the same dataset will be
accessed.Think java servlet is also a java class.
If the data is static,it belongs to the class alone not the instance of the class..
Thanx.
Prabu.U
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Madhav and Prabhu, thanks for your replies.
Sanjana
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And remember, unless you declare your servlet implements SingleThreadModel, there will only be one instance of the servlet which may have several Threads simultaneously executing it.
This means that both instance and static variables are liable to modification every time the servlet is executed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic