In this tutorial you will learn about how to get multiple checkbox values in JSP.
JSP Multiple Checkbox Values
In this tutorial you will learn about how to get multiple checkbox values in JSP.
Checkbox facilitates to select the options. However, Radio button is also facilitates this facility but, in Radio button one can select only one option whereas, using the checkbox one can select more than one options. So wherever you are required to facilitate user that they can select more than one options you may use the checkbox. Checkbox is a input type provided by the HTML tag <input>.
Example :
An example is being given here will demonstrate you about how to use the checkbox and how to get multiple checkbox values in JSP. In this example I have created a JSP page named jspMultipleCheckBox.jsp then designed a form and put the checkboxes with various options and a submit button to submit the form. Then I have written java code using the Scriptlets to retrieve the value of checked checkboxes. In this example I have also used the javascript code to validate the checking of checkbox checked. If you will submit the form without checking any checkbox an alert box will be prompted with a message "Please select your favorite fruit". But when you will checked the all checkboxes you will be prompted an another alert box with message "Click OK to display your favorite fruit list". Then the list of fruits you have checked will be displayed however, a number of fruit list will be displayed the number of checkboxes you have checked even a single checkbox is checked but the second alert box will be not displayed.
jspMultipleCheckBox.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script type="text/javascript"> function checkBoxValidation() { for(var i=0; i < document.form1.fruit.length; i++) { if(!document.form1.fruit[i].checked) { alert("Please Select Your favorite fruit"); return false; } else { alert("Click OK to display your favorite fruit list"); return true; } } } </script> <title>JSP Multiple Checkbox</title> </head> <body> <form name="form1" onsubmit="checkBoxValidation()"> <h3>Select your favorite Fruits</h3> <p><input type="checkbox" name="fruit" value="Mango"/>Mango</p> <p><input type="checkbox" name="fruit" value="Apple"/>Apple</p> <p><input type="checkbox" name="fruit" value="Grapes"/>Grapes</p> <p><input type="checkbox" name="fruit" value="Papaya"/>Papaya</p> <p><input type="checkbox" name="fruit" value="Lychee"/>Lychee</p> <p><input type="checkbox" name="fruit" value="Pineapple"/>Pineapple</p> <p><input type="submit" value="submit"/> </form> <% String fruits[]= request.getParameterValues("fruit"); if(fruits != null) { %> <h4>I likes fruit/s mostly</h4> <ul> <% for(int i=0; i<fruits.length; i++) { %> <li><%=fruits[i]%></li> <% } %> </ul> <% } %> </body> </html>
Output :
When you will execute the above JSP page you will get the output as follows :
1. When the above mentioned JSP page will be invoked page (without checked the checkbox) will be look as follows :
2. And when you will submit the form by clicking on submit button an alert box will be displayed and after clicking OK the alert box the list will be displayed as follows :
[ 0 ] Comments