Java Servlet: How can I retrieve selected radio button values?
Asked Answered
C

4

6

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.

Celik answered 6/4, 2012 at 18:32 Comment(1)
Oh no, why aren't you using a JSP to generate the HTML?Hellenize
I
15

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)) {
    ...
    }
    ...
    
}
Immortelle answered 6/4, 2012 at 18:47 Comment(0)
G
6

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...
}
Gabbi answered 6/4, 2012 at 18:40 Comment(2)
So I am clear, what values are stored in String variables for q1 and q2 on calling the getParameter()?Celik
@Jnanathan: The value you get is whatever is in the value attribute of the <input type="radio">. I've updated my answer to include value attributes.Gabbi
M
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");
Mader answered 6/4, 2012 at 18:38 Comment(0)
M
1

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.

Malversation answered 19/8, 2014 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.