When I click on the submit button, the specified item shoud be added to the item-list/cart. However, I am not getting the result I am expecting.
What is happening is that when the page have loaded, the "onclick" function have executed on all the items in the loop.
How do I resolve this?
What options do I have besides "onclick", which seems buggy?
First item of advice: ditch the Java scriptlets. They've been obsolete for over 12 years and should no longer be used on JSP pages. Learn to use the JSTL and EL instead. See this JspFaq entry.
Advice #2: with your new knowledge of how JSP works, you'll need to sit back and rethink your approach. Your original approach assumed that you'd be able to add to the list in real time as buttons are clicked. Not so. To make any changes to the server, you need to go back with a new request -- either a link, a form or an Ajax request.
All of that requires a major refactor from the start. So don't try to band-aid what you've got; it's just going to lead to frustration. Sit back and think about how you can approach this knowing that once the JSP executes and sends the HTML to ther browser that you've got to wait for a new request to come back to do anything else.