This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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
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).]
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.
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.
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.
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.