Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JSP (http://www.go4expert.com/articles/jsp-tutorials/)
-   -   Getting checkbox value(s) from a HTML form to a JSP page (http://www.go4expert.com/articles/getting-checkbox-value-s-html-form-jsp-t4542/)

Sanskruti 4Jun2007 18:16

Getting checkbox value(s) from a HTML form to a JSP page
 
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

parvez.yu 6Mar2008 17:00

Re: Getting checkbox value(s) from a HTML form to a JSP page
 
nice one i tried it

gkumar 24Jun2009 16:43

Re: Getting checkbox value(s) from a HTML form to a JSP page
 
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!

salman8200 11Aug2009 11:47

Re: Getting checkbox value(s) from a HTML form to a JSP page
 
Nice thanks

natraj 25Aug2009 00:54

Re: Getting checkbox value(s) from a HTML form to a JSP page
 
Really an easy and nice example.

fashionbop 12Mar2010 08:29

Re: Getting checkbox value(s) from a HTML form to a JSP page
 
- -!
So easy?~~!!

alssadi 19Dec2010 21:20

Re: Getting checkbox value(s) from a HTML form to a JSP page
 
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 ?


All times are GMT +5.5. The time now is 05:35.