views:

423

answers:

1

Hi.. I'm developing a jsp/serlvet application. I have a page with a list of inputs as checkboxes . I want to send values of selected buttons to a servlet using ajax/jquery. In the servlet , I want to extract these values and use them .
for example:

  • The user checks some checkboxes , for example, list of messages.
  • He/She presses Delete Link to delete selected messages.

I searched and found something like this :

$("#inboxDeleteSelected").click(function(){
    var data = { 'checkBoxList[]' : []};
    var list=$(":input:checkbox:checked"); // getting all selected checkboxes.
    $(list.each(function() {
       data['checkBoxList[]'].push($(this).val());
    }));
    $.post("servlet?do=deleteSelected",data,function(d){
        // do something here
    });
});

My questions:

  • How can I send selected checkboxes values to a servlet ?
  • How can I extract these values in the servlet ?

Note:

I don't use submit button to submit the selected checkboxes,Indeed I use link/anchor to send those values .

+2  A: 
  1. They are sent using their name, repeated:

    servlet?do=deleteSelected&checkboxGroup=value1&checkboxGroup=value2
    

    You can see that with the following simple html (after you press the submit button, take a look at the address bar):

    <form method="get">
    <input type="checkbox" name="checkboxGroup" value="1" />
    <input type="checkbox" name="checkboxGroup" value="2" />
    <input type="checkbox" name="checkboxGroup" value="3" />
    <input type="submit" />
    </form>
    
  2. You obtain them using:

    String[] values = request.getParameterValues("checkboxGroup");
    
Bozho