It's not a secret anymore!
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

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Static data in Servlets" Watch "Static data in Servlets" New topic

Static data in Servlets

Ranch Hand

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

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..
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

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

Joined: Mar 22, 2000
Posts: 13028
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.
I agree. Here's the link:
subject: Static data in Servlets
jQuery in Action, 3rd edition