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

Discussion in 'JSP' started by Sanskruti, Jun 4, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    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



    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:
    <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
     
  2. parvez.yu

    parvez.yu New Member

    Joined:
    Feb 14, 2008
    Messages:
    100
    Likes Received:
    0
    Trophy Points:
    0
    nice one i tried it
     
  3. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    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!
     
  4. salman8200

    salman8200 New Member

    Joined:
    Aug 8, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
  5. natraj

    natraj New Member

    Joined:
    Aug 24, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Really an easy and nice example.
     
  6. fashionbop

    fashionbop New Member

    Joined:
    Sep 11, 2009
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Shopping from China
    Location:
    Guangzhou, China
    Home Page:
    http://www.fashion-bop.com
    - -!
    So easy?~~!!
     
  7. alssadi

    alssadi Banned

    Joined:
    Dec 11, 2010
    Messages:
    41
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Creative director & web developer
    Location:
    Dubai
    Home Page:
    http://uaeinfographics.blogspot.com/
    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 ?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice