2

I have a success.jsp page which displays multiple rows and columns with an Edit button and a checkbox for each row. If the user clicks on Edit button, the checkbox is selected. Below is success.jsp :

<%@page import="mymvc.model.TableColumns"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%-- 
    Document   : success
    Created on : Jul 8, 2014, 1:43:17 PM
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Login Success</title>
        <script type="text/javascript">
            function validate(n) {
                v = document.getElementById("check"+n)
                v.checked = !v.checked;
                x = document.getElementById("typeId"+n).removeAttribute('readonly');
                y = document.getElementById("paramSeq"+n).removeAttribute('readonly');
                z = document.getElementById("paramName"+n).removeAttribute('readonly');
            }

        </script>
    </head>
    <body>
        <form action="DBController" method="post">

        Welcome ${requestScope['user'].username}.


        <table>
            <tr style="background-color:#f0a64e;">
                <th class="border">ID</th>
                <th class="border">Param Sequence</th>
                <th class="border">Param Name</th>
            </tr>
            <c:forEach var="element" items="${requestScope['listData']}" varStatus="status">
                <tr>
                    <td><input  id="typeId${status.index}" value="${element.typeId}" readonly="true"</td>
                    <td><input  id="paramSeq${status.index}" value="${element.paramSeq}" readonly="true"</td>
                    <td><input  id="paramName${status.index}" value="${element.paramName}" readonly="true"</td>
                    <td>
                        <input id="edit${status.index}" type="button" value="Edit" name="edit" onclick="validate(${status.index})"</input>
                    </td>
                    <td><input type="checkbox" id="check${status.index}" name="selectedItems" value="<c:out value="${status.index}"/>"</td>
                </tr> 
            </c:forEach>
        </table>
        <input type="submit" value="Update" name="update" />
        </form>
    </body>
</html>

I don't know how to send the whole row data to my servlet if more than one row is selected. I am able to get all the indexes of the checkboxes which are selected. But I am unable to extract other related values like typeId, paramSeq and paramName. My servlet is as follows :

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String[] edited = request.getParameterValues("selectedItems");
    //String pName = request.getParameter("paramName"+edited[1]);
    Enumeration<String> paramName = request.getParameterNames();
    String[] param = new String[10];
    int i=0;
    while(paramName.hasMoreElements()){
        param[i]=paramName.nextElement();
    }
    RequestDispatcher rd = null;
    rd = request.getRequestDispatcher("/update.jsp");
    request.setAttribute("param", param);
    request.setAttribute("edited", edited);
    rd.forward(request, response);
}

In the above code, currently, I am trying to get all the parameters passed and the rows which are selected. I want to modify this servlet and access the selected row along with other data like typeId, etc. to create UPDATE statements for every row. I referred this and this but not much help.

Community
  • 1
  • 1
rohit k.
  • 128
  • 1
  • 14

1 Answers1

0

I think what you are missing is that you are giving each input type a unique id but you are not specifying the name of the input types, and in the servlet you are trying to get values by name.

So please add names along with the id's in order to get them in the servlet

        <c:forEach var="element" items="${requestScope['listData']}" varStatus="status">
            <tr>
                <td><input  name="typeId${status.index}" value="${element.typeId}" readonly="true"</td>
                <td><input  name="paramSeq${status.index}" value="${element.paramSeq}" readonly="true"</td>
                <td><input  name="paramName${status.index}" value="${element.paramName}" readonly="true"</td>
                <td>
                    <input id="edit${status.index}" type="button" value="Edit" name="edit" onclick="validate(${status.index})"</input>
                </td>
                <td><input type="checkbox" id="check${status.index}" name="selectedItems" value="<c:out value="${status.index}"/>"</td>
            </tr> 
        </c:forEach>
    </table>
Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23