Getting checkbox value(s) from a HTML form to a JSP page

Sanskruti's Avatar author of Getting checkbox value(s) from a HTML form to a JSP page
This is an article on Getting checkbox value(s) from a HTML form to a JSP page in JSP.
Rated 5.00 By 1 users
This article demonstrates how to get checkbox value(s) from a HTML form to a JSP page. It consists of one HTML page 'sports.html', and one JSP page 'sports.jsp'.

Prerequisites



In order to complete the example application, you should be familiar with the following:
  • HTML
  • Java
  • JSP
This demonstration requires that the following software components are installed on your system and configured correctly:
  • Server such as Apache tomcat, J2EE, JBoss or other
  • Sun JDK version 1.5 or above
HTML file 'sports.html', is a simple form. It includes five checkboxes, each representing a sports such as Cricket, Football, Tennis etc. User can select his/her favorite sport(s) by selecting checkbox(es). Click on 'Submit' button on the form to send value(s) to JSP page 'sports.jsp'.

HTML form: sports.html



Code: HTML
<HTML>
<body>
<FORM method="POST" ACTION="sports.jsp">
<center>
Select your favorite sport(s): <br><br>
<table>
<tr>
    <td>
        <input TYPE=checkbox name=sports VALUE=Cricket>
    </td>   
    <td>   
        Cricket
    </td>
</tr>

<tr>
    <td>
        <input TYPE=checkbox name=sports VALUE=Football>
    </td>   
    <td>   
        Football
    </td>
</tr>

<tr>
    <td>
        <input TYPE=checkbox name=sports VALUE=Tennis>
    </td>   

    <td>   
        Tennis
    </td>
</tr>

<tr>
    <td>
        <input TYPE=checkbox name=sports VALUE=Rugby>
    </td>   

    <td>   
        Rugby
    </td>
</tr>

<tr>
    <td>
        <input TYPE=checkbox name=sports VALUE=Basketball>
    </td>   

    <td>   
        Basketball
    </td>
</tr>
</table>
<br> <INPUT TYPE=submit name=submit Value="Submit">
</center>
</FORM>
</BODY>
</HTML>
JSP page 'sports.jsp' is also very simple. As user may select multiple checkboxes, so to get these multiple values for sports, a string array 'sports[]' is declared. Please note request.getParameterValues() is used to fetch values from checkboxes checked or selected. Now, array 'sports' consists of values for sports which user has selected as favorites. A loop is used to display the favorite sport(s) of user.

JSP page: sports.jsp



Code: Java
<html>
<body>
<%! String[] sports; %>
<center>You have selected:
<%
   sports = request.getParameterValues("sports");
   if (sports != null)
   {
      for (int i = 0; i < sports.length; i++)
      {
         out.println ("<b>"+sports[i]+"<b>");
      }
   }
   else out.println ("<b>none<b>");
%>
</center>
</body>
</html>
If user has selected Cricket, Football, Tennis, then output will be shown as:

Code:
You have selected: Cricket Football Tennis
0
parvez.yu's Avatar, Join Date: Feb 2008
Ambitious contributor
nice one i tried it
0
gkumar's Avatar
Banned
Try using the form to stores, ie. the record id so the checked items can be retrieved from the form.
1. get result set and stores the record ID in the "id" property as a checkbox value, where getstring(1) is the record ID:
while (rs.next()) {
rec = new Sales();
rec.setId("<input type=\"checkbox\" name=\"selectedItems\"
value=\""+rs.getString(1)+"\" />"+rs.getString(1));

2. displays result and check box in the "id" property in jsp:
<display:table class="display1" style="border:1px solid #999"
name="requestScope.salesList" sort="list">
<display:column property="id" title="Row ID" sort="true" />

3. the updateSales form has the "selectedItems" properties
private String[] selectedItems = {};
public String[] getSelectedItems() {
return this.selectedItems;

4. In the action form, retrieves the selected items
String[] idList = updateSales.getSelectedItems();

5. now do whatever you wish with the list of selected items

Good luck!
0
salman8200's Avatar, Join Date: Aug 2009
Newbie Member
Nice thanks
0
natraj's Avatar, Join Date: Aug 2009
Newbie Member
Really an easy and nice example.
0
dipahole's Avatar, Join Date: Nov 2009
Newbie Member
- -!
So easy?~~!!
0
alssadi's Avatar
Banned
what about the selected box ?
can you prvide us with an artical about that , i tryed the (getParameterValues)
it didnt work with me , any help please ?