Change this line:
out.println("<td><input type=\"checkbox\" name=\"checkbox\"></td>");
Count:
out.println("<td><input type=\"checkbox\" name=\"selected\" value=\""+ex.getExpenses().get(i-1)+"\"></td>");
This will create checkboxes that you can identify with the value tag. I hope the number field is the primary key or the like.
Then in the next run. You can get all checked values with:
String[] selected = request.getParameters("selected");
now you can just add numbers. I don’t quite understand what the list of expenses from the code looks like. Here is just an illustration that will not work, but can give you an idea of how to do this:
int sum = 0; for (int i = 0; i < ex.getExpenses().size(); i++) for(int selectedIndex = 0; selectedIndex < selected.length; ++selectedIndex) if (ex.getExpenses().get(i) == Integer.parseInt(selected[selectedIndex])) sum += ex.getExpenses().get(i-1);
source share