failing to retrieve multiple checked value from JSP
Question
I'm trying to retrieve mulitple checked value, which are being showed in JSP dynamically, retrieved from inbox folder of the mail server, just like in yahoo, or gmail. But whenever I select multiple checkboxes(or either one), I'm not able to retrieve their values, like subject and username. What should I have to do in JSP to retrieve those values in another JSP dynamically?
Solution
Each check-box in a form should have the same name but different value. e.g.
...
<input
type="checkbox"
id="SELECT_CB_1"
name="SELECT_CB"
value="1"
/> Checkbox 1
<input
type="checkbox"
id="SELECT_CB_2"
name="SELECT_CB"
value="2"
/> Checkbox 2
<input
type="checkbox"
id="SELECT_CB_3"
name="SELECT_CB"
value="3"
/> Checkbox 3
...
Then when your form gets posted, you can use HttpServletRequest
to retrieve the values of all checked boxes with the name SELECT_CB
.
String[] checked_values = request.getParameterValues( "SELECT_CB" );
You may need to check for null
if no checkboxes are selected.
In the previous example, if you check SELECT_CB_1
and SELECT_CB_3
then checked_values
will contain [ "1", "3" ]
You can use these values then to retrieve other pieces of your puzzle, like subject and username.