How to Get the Value of a Checkbox in JSP

by Mark Stansberry - Updated August 21, 2018
You can use JSP code to get the value of a checkbox.

JSP code, also known as Java Server Pages, is a client side scripting language that provides responses to actions on Web pages. When a checkbox is included in the markup, a JSP script can determine the value associated with the checkbox -- the text that is to the right of the checkbox. The actual JSP code to retrieve the checkbox only takes a few lines of code, and it can be appended to the HTML markup code in your Web page.

Open the HTML page that contains your checkbox markup.

Position your mouse cursor at the line directly after the closing form tag -- "</form>" -- that encloses the checkbox.

Type the JSP script identifier -- "<%" -- used to signify the beginning of the JSP script. Type it at the start of the next line of the file.

Type, starting at the next line in the file, code that will store the value of the checkbox in a string variable named "checkboxvalue." Use the "request.getParameterValues" command. Specify the argument of the getParameterValue method to be the value of the "name" assigned in the "<input type> " statement in your HTML checkbox markup.

String checkboxvalue = request.getParameterValues("firstCheckBox");
if (checkboxvalue.length > 0) { out.println("The Check Box Value of the First Checkbox is "); out.println(checkboxvalue);

In this code, the value in the HTML checkbox markup tag with the name "firstCheckBox" is stored in a string variable named "checkboxvalue" The if then statement checks if the number of characters in "checkboxvalue" is greater than 0, to check if a value was assigned in the checkbox HTML markup.

For this example, the JSP checkbox was specified with the HTML markup code:

<input type="checkbox" name="firstCheckBox" value="Visa Card"> Charge Your Visa<BR>

In this statement a checkbox component is assigned a variable name of "firstCheckBox" that has the value (text string) "Visa Card." stored in it.

Type, starting at the next line of the file, the JSP script identifier used to signify the end of a JSP script -- "%>" -- to close the code.

Save the HTML file and upload the saved version to your website. Navigate to the page and select the checkbox on the page, then click the "Submit" button. The message "The Checkbox Value of the First Checkbox is: Visa Card" will be displayed on the line.

About the Author

Mark Stansberry has been a technical and business writer over for 15 years. He has been published in leading technical and business publications such as "Red Herring," "EDN" and "BCC Research." His present writing focus is on computer applications programming, graphic design automation, 3D linear perspective and fractal technology. Stansberry has a Bachelor of Science in electrical engineering from San Jose State University.

Photo Credits

  • Jupiterimages/ Images
Cite this Article A tool to create a citation to reference this article Cite this Article