Meaningless Drivel is fun!
The moose likes Servlets and the fly likes private varibles in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "private varibles in servlet" Watch "private varibles in servlet" New topic

private varibles in servlet

sitaram irrinki
Ranch Hand

Joined: Feb 16, 2005
Posts: 158
can we write private variables in servlet?if yes how they are managed by multiple threads?
Makarand Parab
Ranch Hand

Joined: Dec 10, 2004
Posts: 121
If it is a private instance variable, that variable will be shared by all the threads. Having a instance variable in servlet is highly discouraged. Well i never define one.
If you define a variable inside the service, dopost, doget methods as it is a local variable for the method, it gets created per thread and is blown off when the method ends.

Hope it makes sense
Let me know if i am wrong.

Makarand Parab
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 364
Makarand is correct. Object vars are not thread safe in a servlet. static final is OK and possibly even variables that are ONLY initialized in the init method and therefore not changed. If this is a concern make them member/method vars where they will be thread safe.

No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
I agree. Here's the link:
subject: private varibles in servlet
It's not a secret anymore!