Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes member variables in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "member variables in servlet" Watch "member variables in servlet" New topic
Author

member variables in servlet

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I have been told we should never use member variables in servlets. Could someone elaborate on this?


SCJP
Visit my download page
Asuthosh Borikar
Ranch Hand

Joined: Sep 29, 2000
Posts: 75
I guess the reason behind this advice is that a single instance of a servlet is used to process multiple requests, which leads to multiple threads running in the servlet. If you are using instance variables, you can't say how the values in these variables are changed by the threads and hence, it's safer not to use instance variables. That's my understanding.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

thanks. that makes sense. I used SingleThreadModel for all my servlets so that was not a concern. But I realize now that is the easy way out and wouldnt work if my site was busier. Time for me to isolate the code that need to be synchronized and fix these 2 problems.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I have some static final member variables. I think they should remain member variables, but all others should be local. Am I correct?
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Randal,
If you want a variable be shared by more than one requests you can store as static final vars. For example a constant.
If you want a variable to be specific to each request, then they must be local vars inside goGet(..) or doPost(..). Which means all instance and static member vars are shared by multiple requests from users.
regds
maha anna
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

that sounds like a yes to me. so I will leave the static final variables as member variables.
it is much more understandable what is going on in the servlets from this one change alone(I havent dealt with synchronization vs SingleThreadModel issue yet). one thing at a time
One of my servlets does a significant amount of processing in both doGet() and doPost(). doPost() calls doGet() as its last action. The servlet is called using Get from one point and called using Post from another. They both use the same variables. It seems I will have to declare them all twice? http://www.angelfire.com/games2/programming/servlets/ShoppingCartServlet.txt

[This message has been edited by Randall Twede (edited January 25, 2001).]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I think the problem is in assuming that all your processing has to happen "inside" the doPost or doGet methods. It's much better to write your processing code in other, private, methods and call those methods from both doGet and doPost, passing in any required data as parameters.
As I have said before - make your methods as small as you can, and only do anything once.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Perhaps the situation I have here is why the "Duke Book Store" tutorial used a ShoppingCart class and a ShoppingCartItem class. They added ShoppingCartItem to ShoppingCart and added ShoppingCart to the session instead of just using the session directly like I did.
Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
hello all! i'm a newbie to servlets and jsp and would like to ask you guys if member variables inside jsps are also affected by the aforementioned threading issues since technically jsps are also servlets.


SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If you declare any variables inside of <% %> or <%= %> tags, then these are all local variables. Because the entire contents of the JSP gets translated into java code and stuffed inside the _jspService method of the servlet.

That is... unless you use the <%! %> tags.

Here is a very simple example of a JSP and what it looks like underneath.The compiled servlet (with heavy comment reduction, as produced by Tomcat 3.2.1)So from here, you can see that 'i' is shared by everyone using the servlet. So the text is very misleading in this case... "you have viewed this page x times" is NOT the case if you and a friend are viewing the page at the same time and hitting F5 (or Ctr-R). If you view the page, and then your friend (on another computer) views it three times... your next viewing will tell you you've viewed it 5 times. So for this example, it should read "this page has been viewed x times" It's more of a hit counter.
Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
great example Mike! thanks a lot!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: member variables in servlet
 
Similar Threads
Error deserializing Object
Question from Exam lab practice Exam 1 about polymorphism
need help..
Native methods/member variables
variable shadowing