This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes ArrayList gets duplicate values in jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "ArrayList gets duplicate values in jsp page" Watch "ArrayList gets duplicate values in jsp page" New topic
Author

ArrayList gets duplicate values in jsp page

Hare Shiva
Greenhorn

Joined: Jul 08, 2012
Posts: 12
i am new to servlet and jsp..I retrieve values from database and set it in a arraylist of bean in servlet.That arraylist of bean is later displayed in jsp page...But after at second run of servlet duplicate values get printed in jsp..Guys help me....

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
You seem to assume that on each request a new instance of the servlet will be created, and hence you'll have a fresh and empty instance of "allData" to work with. That's not how servlets work - there will only ever be a single instance of the servlet class, and it is used to serve all requests.

But you can't just clear out that List before you use it, either, because instance variables are shared between simultaneous requests, so you might be interfering with other requests.

So it's best to avoid instance variables until you have a much better understanding about how concurrency works in web apps. Luckily, that's very easy to achieve in this case.

(I've also added code tags to your post - see how much easier it is to read that way? Please UseCodeTags in the future.)


Ping & DNS - my free Android networking tools app
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

why would you declare inside while?
and use finally clause to close your connections
Hare Shiva
Greenhorn

Joined: Jul 08, 2012
Posts: 12
thanks a lot...all i had to do was clear alldata..its working now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList gets duplicate values in jsp page