Skip to content
Advertisement

Radio Button value retrieval using servlets

I’m quite new to the process of web development, so I apoligise if this seems a really silly question.

Basically, I’m creating a servlet whereby a user will answer ‘yes’ or ‘no’ to three 3 questions, and then submit the form.

What I wish to do is to retrieve the value of each button, and store it a data structure such as an array, where I can perform a check on it. However, I am a little lost at this point. Is there a method in java I can call on each button, something along the lines of getValue()?

A look at the code ive developed so far:

out.println("<FORM ACTION="Game" METHOD = "POST">" +
        "<b>Question 1: Are you over the age of 25?. </b><br> <br>" +

        "<input type = "radio" name = "rad1Q1" onclick = "getAnswer('a')"> Yes " +
        "<input type = "radio" name = "rad2Q1" onclick = "getAnswer('b')"> No<br>" +

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

        "<input type = "radio" name = "rad1Q2" onclick = "getAnswer('a')"> Yes " +
        "<input type = "radio" name = "rad2Q2" onclick = "getAnswer('b')"> No <br>"

Advertisement

Answer

input values in the html form will be submitted as request parameters to the servlet. In the servlet where the form is submitting to, you can do

String rad1Q1 = request.getParameter("rad1Q1");

to get the selected value.

I do suggest you do put the html code in the jsp tho instead of servlet to make the code more clear and easier to maintain.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement