How to read value of Multiple Checkbox submitted via clicked link using JQuery to a Java Servlet File

Posted on

This article is written in order to explain about how to read the value submitted using JQuery from multiple checkbox. The focus is to read the value submitted to a Java Servlet file. The value itself is retrieved from a file either in a HTML file format or a JSP file format. But the most important thing are the value which is going to be retrieved, it is in the form of an array. For an example an array of multiple checkbox selected. There are more than one value which in the end it is actually an array variable submitted to the Java Servlet file.

There are several articles that might useful in order to achieve the purpose specified in the title of this article. It is about how to read the values of multiple checkbox submitted using JQuery. There are few steps need to be taken in order to achieve it. These are the following articles which can be used as further reference for doing it :

1. Make sure that JQuery library file has already been loaded. In order to check further, read the reference in the article titled ‘How to check whether JQuery has been loaded or not’ in this link.

2. In this context of article, the actual process is basically submitting the form with every content in it to a Java Servlet file. Regarding on the topic for submitting form by clicking a link, it can be found as an article titled ‘How to Submit Form using JQuery from a Click Link’ in this link for further reference.

After reading the above articles, try to implement all of them and then pursue on the process of reading the value submitted to the Java Servlet using Query. Below are the snippet codes needed in order to achieve it :

1. The form defined. For an example :

<form id="my_form" action="${pageContext.request.contextPath}/myservlet" method="POST"> 
...
<input id="chkbox" type="checkbox" value="1" />
<input id="chkbox" type="checkbox" value="2" />
<input id="chkbox" type="checkbox" value="3" />
...
<a id="my_link" href="${pageContext.request.contextPath}/myservlet">Send Data</a> 
</form>

2. The JQuery snippet code defined.

Get the form to be submitted by detecting the click event of the link as shown with the following snippet code or script defined :

$('#my_link').click(function () {
      $('#my_form').submit();
});

3. Retrieve the value of the selected HTML Checkbox element in the JSP Servlet file. Below the snippet code for retrieving the value submitted by a HTTP Request POST.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           String selectedCheckbox[] = request.getParameterValues("chkbox");

           for (int i = 0; i < selectedCheckbox.length; i++) {
                System.out.println("Name from doPost : " + selectedCheckbox[i]);
           }

           ....
}

Basically, the main line which is becoming an important key role in order to retrieve the value of selected checkbox is the following line shown above :

String selectedCheckbox[] = request.getParameterValues("chkbox");

The chkbox is an identifier which is used to get the variable name submitted from the form which is indeed the identifier of he HTML Checkbox element.In the end, it is passed to a variable named ‘selectedCheckbox’ which is in the form of array of String. Since there are multiple checkboxes with each of the checkbox is holding its own value, it must be stored into an array variable.

Leave a Reply