File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Static data in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Static data in Servlets" Watch "Static data in Servlets" New topic
Author

Static data in Servlets

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 03, 2000
Posts: 5040

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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Prabu
Greenhorn

Joined: Jun 13, 2001
Posts: 6
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

Joined: Nov 22, 2008
Posts: 18944

Madhav and Prabhu, thanks for your replies.
Sanjana
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12681
    
    5
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.

Java Resources at www.wbrogden.com
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Static data in Servlets
 
Similar Threads
To be stateful of to not be stateful, that is the question
Servlet Context
need help on question from j2eecertificate
mime type in web.xml
Serializing XML Dom Document Object and returning it from an EJB method...