How to Read checkbox value in JSP




Hey guys in this article, you will learn about creating the checkbox and reading the checkbox values in JSP with full code example.

Read More:

Overview


To create a checkbox in HTML we will use <input type="checkbox"> tag. It contains name attribute.

<input type="checkbox" name="checkboxName" value="Sales" />Sales

To read the checkbox value in JSP, we will use the request object

<%= request.getParameterValues("checkboxName") %>

getParameter() takes the name of the checkbox and it return array.

Complete Example


Follow the below steps to understand the complete example

Create a Project


You can check the following post to create a new Dynamic Web Project

Create a HTML form


Create EmployeeForm.html file under WebContent and add the following content

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<title>Employee Form</title>
	</head>

	<body>
		
		<form action="employee-details.jsp">
			
			First Name:
			<input type = "text" name = "firstName" />
			<br/><br/>
			
			Last Name:
			<input type = "text" name = "lastName" />
			<br/><br/>
			Select country:
			<select name = "country">
				<option>India</option>
				<option>Australia</option>
				<option>Germany</option>
				<option>Spain</option>
				<option>Canada</option>
			</select>
			<br/><br/>
			Select Gender:
			<input type="radio" name="gender" value="Male" />Male
			<input type="radio" name="gender" value="Female" />Female
			<br/><br/>
			Select Department:
			<input type="checkbox" name = "departments" value="Sales" />Sales
			<input type="checkbox" name = "departments" value="Marketing" />Marketing
			<input type="checkbox" name = "departments" value="IT" />IT
			<input type="checkbox" name = "departments" value="Support" />Support
			<br/><br/>
			<button type="submit">Submit</button>
			
		</form>
		
	</body>

</html>

Create a JSP file


Create employee-details.jsp file under WebContent and add the following content

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	
	<h1>Employee details</h1>
	<p>First Name: ${param.firstName}</p>
	<p>Last Name: ${param.lastName}</p>
	<p>Country: ${param.country}</p>
	<p>Gender: ${param.gender}</p>
	<p><b>Departments:</b></p>
	<% 
		String[] values = request.getParameterValues("departments");
		for(String department : values) {
			out.println("<P>"+department+"</p>");
		}
	%>
</body>
</html>

Run the file


Right click on the File, choose Run As, choose Run on Server. It will open in a default web browser, you will see the following content
11

12

That’s it for this post, if you like this post, please share this post with your friends and collogues and also share this on your social media profiles.



Partners: maltan kasinot Sweet Bonanza Holland Oranje Jackpot big bass splash demo online spil uden om rofus Golden Euro Casino udenlandsk casino pelaa netticasino udenlandske casinoer Wazamba Casino zimpler bookies

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply