'Java Servlet: How can I retrieve selected radio button values?

I have created a simple servlet in which a user will be presented with 2 questions, answering either true or false. My problem lies in retrieving the answers selected by the user.

Code:

            out.println("<FORM ACTION=\"Game\" METHOD = \"POST\">" +

        "<b>Question 1: Are you over the age of 25? </b><br> <br>" +

        "<input type = \"radio\" name = \"Q1rad1\" onclick = \"getAnswer('a')\"> True " +
        "<input type = \"radio\" name = \"Q1rad2\" onclick = \"getAnswer('b')\"> False<br>" +

        "<br><br><b>Question 2: Are you from earth?</b><br> <br>" +

        "<input type = \"radio\" name = \"Q2rad1\" onclick = \"getAnswer('a')\"> True " +
        "<input type = \"radio\" name = \"Q2rad2\" onclick = \"getAnswer('b')\"> False<br>" +

        out.println("<Center><INPUT  TYPE=\"SUBMIT\"></Center>");


        );

Each question has 2 radio buttons, Q1rad1 & Q2rad2, for answering True or False. How can i know the value selected by each user when the submit button is pressed.

I understand it may be more efficient when using Javascript but for the purposes of this problem I must be using servlets.



Solution 1:[1]

You have to define the value you want to retrieve when the radio button is selected

The value setting defines what will be submitted if checked.

The name setting tells which group of radio buttons the field belongs to. When you select one button, all other buttons in the same group are unselected.

<input type="radio" name="Q2" onclick="getAnswer('b')" value="b">
<input type="radio" name="Q2" onclick="getAnswer('a')" value="a">

In your Servlet which will recieve the request you'll have something like

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // get the value of the button group 
    String q2 = request.getParameter("Q2");
    // compare selected value 
    if ("a".equals(q2)) {
    ...
    }
    ...
    
}

Solution 2:[2]

You haven't named your radio buttons correctly. Each radio option for the same question need the same name attribute. Also, you should have a value attribute on each <input type="radio">. I'm not sure you need the onclick handler at all. You should also have a </form> closer tag. Your form might look like this:

 out.println("<form action=\"Game\" method=\"POST\">" +

    "<b>Question 1: Are you over the age of 25? </b><br> <br>" +

    "<input type = \"radio\" name = \"Q1\" value=\"True\"> True " +
    "<input type = \"radio\" name = \"Q1\" value=\"False\"> False<br>" +

    "<br><br><b>Question 2: Are you from earth?</b><br> <br>" +

    "<input type = \"radio\" name = \"Q2\" value=\"True\"> True " +
    "<input type = \"radio\" name = \"Q2\" value=\"False\"> False<br>" +

    "<Center><INPUT  TYPE=\"SUBMIT\"></Center>" +

    "</form>"
    );

And then in the doPost() method of servlet that handles the form submission, you can access the values using request.getParameter(). Something like this:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String q1 = request.getParameter("Q1");
    String q2 = request.getParameter("Q2");
    // more processing code...
}

Solution 3:[3]

Give the same name to the radios of the same question, and set different values. Look at this page.

Then in the request you will get a parameter with the name of the radio group and the value selected. After submit the servlet the receives the post can use:

String value = request.getParameter("radioName");

Solution 4:[4]

For your HTML Code the below lines are enough

protected void doPost(HttpServletRequest req,HttpServletResponse res){
String q1 = request.getParameter("Q1");
String q2 = request.getParameter("Q2");`
}

For example, Considering your HTML Code.

If Q1 is pressed

"TRUE"

then it would be our "Input" in Servlet.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user14773854
Solution 2
Solution 3 dash1e
Solution 4 Balaji