dimanche 25 novembre 2018

Get checkbox values from JSP Page

I'm trying to get all the values from a list of checkboxs in a JSP Page, to a Java Class. That's My JSP Page:

    <table border="1" cellpadding="5" cellspacing="1" style="width: 877px; ">
   <tr>
      <th>Code</th>
      <th>Name</th>
      <th>Price</th>
      <th>Select</th>
      <th>Edit</th>
      <th>Delete</th>
      <th>Show</th>
   </tr>
   <c:forEach items="${productList}" var="product" >
      <tr>
         <td>${product.code}</td>
         <td>${product.name}</td>
         <td>${product.price}</td>
         <td>
         <input type="checkbox" name="ProductItem"  value="${product.code}">
             <c:url value="ShowProductList" var="url">
             <c:param name="ProductItemP" value="${product.code}"/>
             </c:url> 
         </td>
         <td>
            <a href="editProduct?code=${product.code}">Edit</a>
         </td>
         <td>
            <a href="deleteProduct?code=${product.code}">Delete</a>
         </td>
         <td>
            <a href="ShowProduct?code=${product.code}">Show</a>
         </td>
      </tr>
   </c:forEach>
</table>


 <a href="${url}">Show Items</a>

As you can see there is a Table with a list of items with a check box in each line. At the end of the table there is button "Show Items" that triggers the user's request. That's the servlet class that perform the request:

    @WebServlet(urlPatterns = { "/ShowProductList" })
public class ShowProductList extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ShowProductList() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Connection conn = MyUtils.getStoredConnection(request);

             String[] paramValues = request.getParameterValues("ProductItemP");
             System.out.println(paramValues.length);
             response.sendRedirect(request.getContextPath() + "/productList");      
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

When an user click on the button I can have only the last product code with the checkbox clicked. If I Try `request.getParameterValues("ProductItem"); I have a null value. I'd prefer to not hove code in the JSP Page (if it is possible.) Can someone help me find a solution ? Thanks for your patience.

`




Aucun commentaire:

Enregistrer un commentaire