How to pass check boxes data using JSP?

JSPJava 8Object Oriented ProgrammingProgramming

Checkboxes are used when more than one option is required to be selected.

Following is an example HTML code, CheckBox.htm, for a form with two checkboxes.

      <form action = "main.jsp" method = "POST" target = "_blank">
         <input type = "checkbox" name = "maths" checked = "checked" /> Maths
         <input type = "checkbox" name = "physics" /> Physics
         <input type = "checkbox" name = "chemistry" checked = "checked" /> Chemistry
         <input type = "submit" value = "Select Subject" />

The above code will generate the following result −

 Maths  Physics  Chemistry

Following is main.jsp JSP program to handle the input given by the web browser for the checkbox button.

      <title>Reading Checkbox Data</title>
      <h1>Reading Checkbox Data</h1>
         <li><p><b>Maths Flag:</b>
            <%= request.getParameter("maths")%>
         <li><p><b>Physics Flag:</b>
            <%= request.getParameter("physics")%>
         <li><p><b>Chemistry Flag:</b>
            <%= request.getParameter("chemistry")%>

The above program will generate the following result −

Reading Checkbox Data

  • Maths Flag :: on

  • Physics Flag:: null

  • Chemistry Flag:: on

Published on 20-Mar-2019 14:19:35