tags:

views:

65

answers:

1

How to get cell data of a specific row from the dynamically created HTML table in JSP?

I am creating JSP Page in the following way

  1. Connect to MySQL Databse
  2. Fetch rows from table based on criteria
  3. Construct HTML table dybamically based on the rows returned in step 2
  4. The first column of table contains checkbox
  5. JSP page contains a Submit button
  6. Select checkbox for some row(s)
  7. On Submit button click, How can i check which row checkbox is selected?
+1  A: 

Give all checkboxes the same name, but a different value, e.g. the row ID.

<table>
    <c:forEach items="${list}" var="row">
        <tr>
            <td><input type="checkbox" name="rowid" value="${row.id}"></td>
            <td>${row.name}</td>
            <td>${row.value}</td>
            ...
        </tr>
    </c:forEach>
</table>

Then you can obtain the checked ones in the server side using HttpServletRequest#getParameterValues() as follows:

String[] rowids = request.getParameterValues("rowid");
// ...
BalusC