Updated February 4, 2023
Introduction to JSP Checkbox
Checkbox is one of the UI element is also called as tickbox, tick box, etc is one of the GUI widgets that will permit the users to make the binary format choice it’s mainly used as either true or false that also enable or disable view in the application. The checkbox also the option like a binary choice the user may have to answer whether the request is yes or no based on the application scenario it will be enabled in this feature. The jsp also the UI widget like checkbox, text box, scroll panels, scroll box, etc the functionalities or actions of the jsp elements are the same as the jsp checkbox are the same as other checkbox UI elements.
Syntax:
The general syntax of the jsp using check box UI elements the html tags will be used for the jsp pages as below format.
<html>
<body>
<form action=".jsp" method="get">
<input type="checkbox" name="" vaue="">
---some input type html UI elements—
</form>
---some jsp logic codes—
</body>
</html>
Above code, we will see UI elements of the html tags will be coordinated and passed the id in the jsp using html forms jsp codes will be handles those values and authenticated with code logic.
How does JSP Checkbox Work?
The jsp checkbox used in the web pages it allows the user to select more than one values in the options list when we are using multiple checkboxes it may be varied upon the values and also meanwhile it will print the user-defined values as the message that will be specified with the view page after submitting the html forms. After submitting the button values the action button is performed it goes to the jsp page and also the values are called through the method like request.getParameterValues(“”) it is used to return the values of the selected checkboxes. Using some java logic codes the application reqUIrement is to be achieved.
Generally, checkbox have different types like standard checkbox, radio checkbox, slider, toggle, read-only, checked, indeterminate, and disabled checkboxes. A checkbox is always like a square box type and it allows for one or more items has to be selected. If we want to set the user values as the list type that time it needs multiple options is to be selected using checkboxes. Whenever the user wants to get more information’s that time the checkboxes needed. When clicked the checkboxes the checkmark also enabled if the application has reqUIred for the user purposes then it has the meaning affirmative choice when clicked again for those checkboxes the tick mark is disabled then it becomes a negative choice like no option. Whenever we have to submit the checkboxes values the form tag is reqUIred for the navigation web page forms then the requested values form is submitted and sent it to another navigation script languages like php,jsp, python, etc. Then the checkbox is always designed like square boxes and it allows for more than one or more item values is to be selected if it uses radio button then the values are in circle format that only appears for one option value is to be selected when we create the values as a list then it needs multiple options to be stored in the single checkboxes, if we want to select the one values among the multiple options in the checkboxes then it used as a radio button feature. When we use the radio button and checkboxes it is mostly user-friendly UI option in jsp web page. It depends upon the style we will use both UI feature in the single web page the checkbox attribute value has their own style in for all the UI codes like html,jsp, angular, etc.
Examples of JSP Checkbox
Following are the examples of jsp checkbox:
Example #1
<html>
<h2>Select Options:</h2>
<form ACTION="second.jsp">
<input type="checkbox" name="id" value="first"> first<BR>
<input type="checkbox" name="id" value="second"> second<BR>
<input type="checkbox" name="id" value="third"> third<BR>
<input type="checkbox" name="id" value="fourth"> fourth<BR>
<input type="checkbox" name="id" value="five"> fifth <BR>
<input type="submit" value="Submit">
</form>
<%
String s[] = request.getParameterValues("id");
if (s != null && s.length != 0) {
out.println("You have selected the option is: ");
for (int i = 0; i < s.length; i++) {
out.println(s[i] + "\n" + "Thank you");
}
}
%>
</html>
Output:
Example #2
Code:
<%@ 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 demo()
{
for(var i=0; i < document.form1.subject.length; i++)
{
if(!document.f.subject[i].checked)
{
alert("Please Select Your options");
return false;
}
else
{
alert("Click Submit to Know your choices");
return true;
}
}
}
</script>
<title>Sample</title>
</head>
<body>
<form name="f" onsubmit="demo()">
<h3>Please Select your Choices</h3>
<p><input type="checkbox" name="subject" value="first"/>first</p>
<p><input type="checkbox" name="subject" value="second"/>second</p>
<p><input type="checkbox" name="subject" value="third"/>third</p>
<p><input type="checkbox" name="subject" value="fourth"/>fourth</p>
<p><input type="checkbox" name="subject" value="fifth"/>fifth</p>
<p><input type="submit" value="submit" />
</form>
<%
String subject[]= request.getParameterValues("subject");
if(subject != null)
{
%>
<h4>You selected options </h4>
<ul>
<%
for(int i=0; i<subject.length; i++)
{
%>
<li><%=subject[i]%></li>
<%
}
%>
</ul>
<%
}
%>
</body>
</html>
Output:
Example #3
Code:
<%@ 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 sample()
{
for(var i=0; i < document.form1.frUIt.length; i++)
{
if(!document.f.shirt[i].checked)
{
alert("Please Select Your favorite tshirts size");
return false;
}
else
{
alert("Click OK to display your choice");
return true;
}
}
}
</script>
<title>Demo</title>
</head>
<body>
<form name="f" onsubmit="sample()">
<h3>Select your favorite T-Shirts Size</h3>
<p><input type="checkbox" name="shirt" value="S"/>S</p>
<p><input type="checkbox" name="shirt" value="M"/>M</p>
<p><input type="checkbox" name="shirt" value="L"/>L</p>
<p><input type="checkbox" name="shirt" value="XL"/>XL</p>
<p><input type="checkbox" name="shirt" value="XXL"/>XXL</p>
<p><input type="submit" value="submit"/>
</form>
<%
String s[]= request.getParameterValues("shirt");
if(s != null)
{
%>
<h4>My Favorite t-shirt size</h4>
<ul>
<%
for(int i=0; i<s.length; i++)
{
%>
<li><%=s[i]%></li>
<%
}
%>
</ul>
<%
}
%>
</body>
</html>
Output:
Conclusion
The checkbox options facilitate selecting the user choice values as the option choice when compared to the radio button feature checkbox will select more than one value in the single checkbox options so the user reqUIrement will be more achievement the checkbox has the input type in the html tag.
Recommended Articles
This is a guide to JSP Checkbox. Here we also discuss the Introduction and how does jsp checkbox works along with different examples and its code implementation. You may also have a look at the following articles to learn more –