• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList gets duplicate values in JSP

 
Sree Mami
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We would need to see your code in order to help you.

Before posting it, be sure to read:
UseCodeTags.
 
Sree Mami
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sree Mami
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using declaration scriplets creates instance variables which are shared with across all threads serving the JSP.
 
Sree Mami
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, do u mean I should avoid declaring variables in <%! %> and use <% %> rather!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your comments!

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic