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 instance variables of the servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "instance variables of the servlet" Watch "instance variables of the servlet" New topic
Author

instance variables of the servlet

Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340
Hi all !
A single servlet instance creates a new thread for every client request.So there can be danger of simultaneous access to instance variables.
So do you think that it is a good idea to keep all the variables as local variables i.e in the doGet/doPost methods.
I'm a little confused on this.
Regards
Danish
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
1. Actually the servlet engine typically re-uses Threads, but as you say, instance variables will be "simultaneously" visible to many request processing Threads.
2. Any variable that changes with each request should NOT be an instance variable in the servlet class. You can use local variables, variables attached to a session, etc. - If you have more than a few variables that carry user specific data, consider making a utility class to keep track of the data. For simple servlets, a session is usually sufficient.
Bill


Java Resources at www.wbrogden.com
Cory Wilkerson
Ranch Hand

Joined: Aug 14, 2001
Posts: 84
So, is this on a per client basis here or are you suggesting that a single servlet instance is created by the servlet engine to process all clients/all requests? I'm not familiar enough with the servlet specification to argue otherwise, it's just always been my assumption that a unique instance of a particular servlet was created on a per client basis (not on a "per request" basis).
Just looking for some clarification -- and thank you.
Cory Wilkerson
Ranch Hand

Joined: Aug 14, 2001
Posts: 84
After reading the Servlet spec...I stand corrected. So, I'm assuming that when JSP developers declare variables throughout the page, they're method local to doPost/doGet once the servlet is compiled?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
Whether or not a JSP variable is an instance variable or a local variable depends on where and how it is declared. If you are experimenting with JSP, take a look at the generated java files, it is very instructive.
You will also find the JSP specs very informative.
Bill
 
jQuery in Action, 2nd edition
 
subject: instance variables of the servlet
 
Similar Threads
Servlet life : when is it safe to use class var's in servlet
confusion in heap and stack - PLEASE HELP
Cookie problem - cookie shared between 2 external seperate networks
Dealing with multi-threading in servlets
Refresh Using setheader on response