It's not a secret anymore!
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


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
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
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.)
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: ArrayList gets duplicate values in jsp page
 
It's not a secret anymore!