• Post Reply Bookmark Topic Watch Topic
  • New Topic

How List made static can affect the application  RSS feed

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made a servlet, where i have declared a List as static, however, i am clearing the list after the end of that servlet. Its running fine, but when i send request from different browsers at the same time, it doesn't works as expected.


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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. dont put business logic directly in Servlet. seperate model from controller
2. declaring instance filed for a servlet will ruin your servlet(most of the time)
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman Venkatasamy


dont put business logic directly in Servlet. seperate model from controller


That means i should separate pagination code in another class, and call it from servlet. Then i won't have to create Servlet object. Any other suggestions please
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote: Then i won't have to create Servlet object.

container takes care about creating/destroying a servlet. you no need to worry about it
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i need to identify different users. So, should i go for some map classes
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is best not to define either static or member variables in a servlet. Preferably, you should keep all your data either on the stack on in session/request/application-scope objects.

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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim is correct. What was your reason for using static in the first place? It makes no sense?

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.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim Holloway, Bear Bibeault.

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
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
So just
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
So just


Ummmmmmmm.

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,
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim Holloway
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!