JSP and HTML Forms – 2

This entry is part 8 of 14 in the series JSP Course

Hi, so in this tutorial we are going to discuss how to read data from HTML forms, containing:

  • Radio Buttons
  • Check boxes

Let’s start with Radio Buttons.

Radio Buttons

For the purpose of this tutorial, we are going to regard the same form we used in the last tutorial but we will be replacing the dropdown list with a couple of radio buttons. The form is going to look like this:

JSP_Tutorial_1

When the information is submitted, we are going to generate a confirmation page with the information entered in the form above.

Coding the Form:

We have already seen how to create the text fields, here is the code for adding the radio buttons to the form:

Let’s add this code snippet to the complete form, so that we can have a better idea of how to put it all together.

See this is pretty straight-forward. We just added radio buttons to the form we have been playing with for quite a while. Now let’s code the JSP page.

Coding the JSP Page:

In the same project, create a new page by the name of “student-radio-response.jsp”. In this file, write the following code:

 

Now when you try to test it, make sure to run the form we defined above. Enter the information in the form and you’ll see the JSP page displaying the information entered into the form.

Check Boxes

With radio buttons, you can only select one at a time, but with check boxes, you can select all the boxes.

Coding the Form:

We are just going to replace the radio buttons we created in the last form with checkboxes. The rest of the information and the form remains the same.

JSP_Tutorial_2

The code for checkboxes:

Let’s put it together with the form:

So now that our form is created, now comes the tricky part. Let’s code the JSP page:

Coding the JSP Page:

Now we won’t be able to use the shortcut” ${param.variable}” style of coding, since the user can check multiple checkboxes. We are going to have to go back to our roots and make use of “request.getParameterValues()”. In this case we’ll be using:

What we are doing here is that we are creating an array, which will contain the values of all the checked checkboxes. The request command will loop through the array the number of times, the amount of checkboxes have been checked and will add the values to the array. In the “student-chckbox-response.jsp” we’ll make use of the JSP scriptlet and add the following the code:

Explanation

See in the scriptlet, what we are doing is. With this line:

We are fetching the values of all the checked checkboxes and storing them in the langs array. Then to display the fetched values we are making use of a “for” loop to iterate through the array and display every value:

So that is all about reading data from HTML forms. From the next tutorial, we are going to learn state management in JSP.

Series Navigation<< JSP & HTML Forms – 1State Management in JSP >>

Leave A Comment

Your email address will not be published. Required fields are marked *