I have a servlet with MySQL database in it. It looks like this: here is the piece of code for it: out.println('
Solution 1:
First of all, you should learn about JSPs and generate your HTML markup from a JSP rather than from the servlet.
Now for your problem. Each of these rows comes from a database table. So each of these rows should have an ID (primary key). Assign the ID of the row to the value of the checkbox. When you submit your form, the servlet will receive all the IDs of the checked checkboxes. Get the values corresponding from these IDs from the database, and sum them (or execute a query that computes the sum directly):
now, you can just add up the numbers. I don't quite understand how you Expenses-list looks like from the code. Here is just an illustration that will not work but could give you the idea on how to do it:
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);
Share
Post a Comment
for "Getting Checkbox Value(s) From A Servlet"
Post a Comment for "Getting Checkbox Value(s) From A Servlet"