Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 321
Php Pete Avatar asked Apr 06 '12 18:04

Php Pete


People also ask

How do you find the value of a radio button group?

Answer: Use the jQuery :checked selector You can simply use the jQuery :checked selector in combination with the val() method to find the value of the selected radio button inside a group.

Which method can be used to obtain the value associated with the selected radio button?

Get the value of selected radio button: querySelector() The querySelector() function is a DOM method of JavaScript. This method is used to get the element that matches with the specified CSS selector in the document. Remember you need to specify the name property of the radio button in HTML code.

How can I get radio button checked?

You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .

How can I know which radio was clicked or selected?

To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method. This returns the name of the option that is currently selected.


2 Answers

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)) {
    ...
    }
    ...
    
}
like image 166
andih Avatar answered Nov 14 '22 21:11

andih


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...
}
like image 36
Asaph Avatar answered Nov 14 '22 21:11

Asaph