Discussions

Web tier: servlets, JSP, Web frameworks: Submit action with a list of checkbox from JSP

  1. Hi All,

    In JSP page I need to show records with the checkbox. The user ticks one or more records that need to delete and clicks the submit button as one action. At the server side, the servlet should extract all the ticked rows and perform delete task. This exercise is to avoid multiple delete action by the User. Now my question is how to store the checked rows in the page and send to server to process. Any help on this is appreciated.

    Thanks in advance
    Jo
  2. Give every checkbox the same name, but different values:

    <input type="checkbox" name="selection" value="1">
    <input type="checkbox" name="selection" value="2">
    <input type="checkbox" name="selection" value="3">

    In the servlet/JSP, use request.getParameterValues() with the checkbox name. You will get an array of strings containing the values of the selected checkboxes.

    String[] selections = request.getParameterValues("selection");
    // If the user checked boxes one and three, this array will be {"1", "3"}

    If you make the checkbox values the primary key for each record, the delete operations should be easy.
  3. Thank you Paul Strack,

    I am implementing as per your example. How about if I use struts form?

    Regards
    Jo
  4. My Struts is kind of rusty. A quick survey of the Struts website indicates that the multibox tag may do what you want.

    However, I have found that with these kinds of controls, it is easier to generate your UI elements "manually" rather than using framework control (which rarely handle this kind of complex UI element just the way you need it to).