How to get Multiple Selected Checkbox Value in Java Servlet - onlyxcodes

Thursday, 25 January 2018

How to get Multiple Selected Checkbox Value in Java Servlet

Hi coder, In this tutorial, you will see how to get all the value of the multiple selected checkbox in Servlet. In this example, a form contains some checkboxes, Users have checked them and when they hit the submit button, send your form to the Java Servlet class and this class will show the value of multiple checkboxes.

How to get Multiple Selected Checkbox Value in Java Servlet


Project Set Up In NetBeans IDE

NetBeans IDE: Download. In this editor, I set this project source code and execute it in this editor too. See below for the structure of the project directory.

Project Set Up in NetBeans IDE

index.html

See below the HTML form consisting of a single text box and four checkboxes, each representing a language name for programming.

method="post" attribute send form data to the server as an HTTP POST request. And action=" MultipleForm" attribute specifies the relative URL of the Servlet file responsible for handling the posted data from that form.

Note: The checkbox name attribute must be the same name and have to be defined with an array. This can be achieved by using [ ] at the end of the checkbox's name attribute.

<form class="form-basic" method="post" action="MultipleForm">

            <div class="form-title-row">
                <h1>HTML Form </h1>
            </div>

            <div class="form-row">
                <label>
                    <span>Your Name</span>
                    <input type="text" name="txt_name">
                </label>
            </div>

            <div class="form-title-row">
                <label>
                    <span>Language</span>
                    <input type="checkbox" name="chk_language" value="java"> JAVA 
                    <input type="checkbox" name="chk_language" value="php"> PHP  
                    <input type="checkbox" name="chk_language" value="asp"> ASP 
                    <input type="checkbox" name="chk_language" value="c++"> C++ 
                </label>
            </div>

               <input type="submit" name="btn_submit" value="Submit">
         
        </form>

 

Form Like Show This Type:-

HTML form consisting of a single text box and four checkboxes

MultipleForm.java

Create a Java Servlet class that is mapped to the URL: MultipleForm, as mentioned in the attribute for action form.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/MultipleForm"}) //it is annotations
public class MultipleForm extends HttpServlet 
{

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
       response.setContentType("text/html"); 
       PrintWriter out=response.getWriter();
       
       String heading="Get Multiple Selected Checkbox Value in Servlet";
       out.print("<h2>" +heading+ "</h2>");
       
        if(request.getParameter("btn_submit")!=null) //check button click event
        {  
            String name=request.getParameter("txt_name"); //get textbox name "txt_name" 
            
            out.println(name); 
            
            out.println("</br>");
            
            String language[]=request.getParameterValues("chk_language"); //get checkbox name value "chk_language" and store in language[] array          
           
            for(int i=0;i<language.length;i++) //apply loop for fecth multiple checkbox value
            {
              out.println(language[i]+","); //fetch with comma separate
            }
        }
    }

}


@WebServlet annotation defines the servlet URL in front of the class. If the user submits the form, the servlet container will execute the doPost method for the servlet. We will perform the following tasks within doPost method.

get text box value in Servlet code

String name=request.getParameter("txt_name"); //get textbox name "txt_name" 
out.println(name);

Explanation :

The getParameter() method is used to get user-specified text box values that pass by the form.

get multiple selected checkboxes values in Servlet Code


String language[]=request.getParameterValues("chk_language"); //get checkbox name value "chk_language" and store in language[] array          
           
for(int i=0;i<language.length;i++) //apply loop for fecth multiple checkbox value
{
    out.println(language[i]+","); //fetch with comma separate
}

Explanation :

Use request.getParameterValues() method to get multiple selected checkboxes values from the "chk_language" attribute name of checkbox. And to store string type variables in the language array.

length – Using this method to find the length of the variable and you can obtain the size of the array. This method can be used for int[], double[], String[].

Apply for loop, the condition i < language.length used to find the variable length of the language array variable that increases by the integer type variable i.

Use of the method out.println() that displays a value or length separated by the comma symbol.

Output

Invoke project in NetBeans IDE the editor will automatically open the browser type any name in the text box and check the checkboxes for some programming language names. I've tested 2 checkboxes.

See results get value for the text box and multiple values for selected checkboxes.

web browser results get value for the text box and multiple values for selected checkboxes.

Download Codes

No comments:

Post a comment