File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList gets duplicate values in jsp page

 
Hare Shiva
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why would you declare inside while?
and use finally clause to close your connections
 
Hare Shiva
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot...all i had to do was clear alldata..its working now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic