aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Dont declare global variables in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Dont declare global variables in Servlet" Watch "Dont declare global variables in Servlet" New topic
Author

Dont declare global variables in Servlet

D.R.Vinayak
Greenhorn

Joined: Mar 27, 2007
Posts: 20
Hi guys,
This is really interesting,don't declare global variables in Servlet or Action Servlet.If you declare Name as a global variable , if user 'A' log's in and set his Name in the variable ,in the mean time if user 'B' log's in and set his name in variable,then user 'A' will have same value in global variable as that of 'B'.Reason,Container always creates a new thread for every request,this thread only execute service method,so the global variables are global for all the user..So avoid using global variables in Servlet...
If i m not right please correct me...according to my observation i faced this problem in my application and overcome by removing global variable.


SCJP 95%<br />SCWCD(waiting for the big)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

What are you calling global variables ? Static variables ?


[My Blog]
All roads lead to JavaRanch
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
Hi Vinayak,

Kindly illustrate an example for us?

Regards


Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
D.R.Vinayak
Greenhorn

Joined: Mar 27, 2007
Posts: 20
When user log's in to our system we used to save his information like first name last name etc.we used global variable for that,since we are using that variables across different function,Now suppose user 'A' logs in his information will be saved in this variables,im mean time if user 'B' logs in his information will be over ridden so ,user who logged in first will loose his information,and his name will change to that of 'B'
D.R.Vinayak
Greenhorn

Joined: Mar 27, 2007
Posts: 20
not static variable
Adithya Menon
Greenhorn

Joined: Jan 21, 2007
Posts: 19
instance variable are shared by all the threads.unlike the localvariables they are not thread safe.


SCJP1.4(96%) SCWCD1.4(92%)
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

They are NOT global variables but instance variables. As they are available to any thread and for every request there is a new thread, the chances are high to have an ambiguity on the variable's value.

Instead you can have a local variable in either of doGet() or doPost() method which are definitely threadsafe since every thread will get its own copy.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dont declare global variables in Servlet