This doubt is related to one of my topics posted under JSP forum, with name as Pagination Problem. The code is related to same pagination, but instead of implementing it in jsp, i changed the code, and implemented in Servlet.
Here is the code
The problem with this code is that i am using static List. If i am using two different browsers at a time, i.e making two requests at a time, it is making a problem. I am showing 2 Q at a time, total of 4 Questions. If in 1st browser, 2 Questions has been displayed, the problem is that the request through second browsers is affected, since the List is static. What should i do now?
Should i clear the list after each navigation? But, i guess, that won't solve the problem.
However, if you do define a servlet variable, you should make it synchronized. That will prevent unexpected alterations of the data when 2 or more clients are concurrently using the servlet.
If you have something that needs to be retained and is unique to each visitor, that's what the session is for. If it's something that's common across all users, that's when to use application scope.
Static variables in a servlet are something that should almost never be used unless they are final constants.
I have changed my code. I removed the pagination code, created a separate class, and removed all static variables. Still testing, whether its working or not.
Kunal Lakhani wrote:It works. Now, how should it recognize different users? Can it be done by just creating HttpSession object. I read that it provides a unique id itself internally and recognizes each users by that.
I think it's time you found yourself a good book on J2EE.
Yes, you can create session objects and they are, in fact, the place to hold information on a per-user basis, since the request.getSession() methods will return an HttpSession that's unique for each user. But there's considerably more to it and the best way to learn how to do it properly is to grab hold of some basic study materials. We're not really set up for step-by-step instruction here. More for answering the questions that come up when you learn the basics and start to apply them,