Your problem is confusing server-side scriptlets with client-side JavaScript.
The following code is going to execute every time the page is loaded:
<%
ArrayList<Resource.Watch> list = (ArrayList<Resource.Watch>) request.getSession()
.getAttribute("list");
list.clear();
%>
Your assumption is that it's only going to execute as part of the JavaScript function, which is incorrect. You can verify this by viewing your rendered HTML, where you will see an empty if
block.
A better approach would be to do something like executing an Ajax call to a servlet method that does the same thing, instead of putting this code directly into your JSP.