File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes ArrayList gets duplicate values in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "ArrayList gets duplicate values in JSP " Watch "ArrayList gets duplicate values in JSP " New topic
Author

ArrayList gets duplicate values in JSP

Sree Mami
Ranch Hand

Joined: Jul 13, 2006
Posts: 75
Hi all

I have lots and lots of doubts in JSP/Servelts. I am happy to see this site members responding so quickly..



I am retrieving some values from database into a Drop down list box say "Employee" based on the value selected I am passing a query again to the database to get value into another Drop down list box. The problem is when the page refreshes after loading values into the second list box, first list box options gets doubled (Arraylist items gets duplicated and it gets populated as and when I refresh the screen) I have stored the value of the selected option into a session and passed that to the jsp page and cleared the ArrayList of first list box and now I got a new problem whenever page refreshes the first list box is empty (as I cleared the ArrayList at the end) how should I retain the values in the drop down list boxes without getting duplicate values for every screen refresh?

Even I am getting ArrayIndexOutofBound exception at some index position.

Please clarify as I am very new to JSP/Servlets..

Thanks a lot in advance.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

We would need to see your code in order to help you.

Before posting it, be sure to read:
UseCodeTags.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sree Mami
Ranch Hand

Joined: Jul 13, 2006
Posts: 75



This is my code.. Where I am getting duplicate values in the array list which I am using to store values in the dropdown list box "department".

Can u please clarify!
[ July 20, 2006: Message edited by: Sree Mami ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Most likely a threading issue.

You used:



which creates one instance of the array list which is shared amongst all instances of the JSP. Is that what you wanted?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sree Mami
Ranch Hand

Joined: Jul 13, 2006
Posts: 75
Thanks all!

I could resolve this issue. Actually whenever I am getting values from the database into the ArrayList, first I am clearing it so that the previous values will not exist anymore.

Once again thanks for your help!!!


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

If you are still using the <%! %> notation to make your variable declarations, then you still have a serious problem with your JSP which will cause perplexing and miserable failures as soon as simultaneous access to the JSP takes place.
Sree Mami
Ranch Hand

Joined: Jul 13, 2006
Posts: 75
Hi Bear Bibeault

I didn't get your point:

If you are still using the <%! %> notation to make your variable declarations, then you still have a serious problem with your JSP which will cause perplexing and miserable failures as soon as simultaneous access to the JSP takes place.


If I shouldn't declare variables in <%! %> what else should I do, to avoid the problem which you were discussing?

Can you explain me the problem declaring variables in "Declaration Tag" in detail!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Using declaration scriplets creates instance variables which are shared with across all threads serving the JSP.
Sree Mami
Ranch Hand

Joined: Jul 13, 2006
Posts: 75
Then, do u mean I should avoid declaring variables in <%! %> and use <% %> rather!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Please use real words in your posts rather than "u".

In my opinion, if you're using JSP 2.0 you shouldn't be using scriptlets at all. But if you are going to employ scriptlets, avoid the use of declaration scriptlets for anything that's not intended to be shared across all threads.
[ July 22, 2006: Message edited by: Bear Bibeault ]
Sree Mami
Ranch Hand

Joined: Jul 13, 2006
Posts: 75
Thanks a lot for your comments!

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Sree Mami:
Then, do u mean I should avoid declaring variables in <%! %> and use <% %> rather!


Yes.
Otherwise all requests to your servlet will share the same variable and you will have threading issues.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList gets duplicate values in JSP