Java Servlet: how can I get selected switch values?

I created a simple servlet in which the user will be presented with 2 questions that answer either true or false. My problem is getting the answers selected by the user.

the 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>");


        );

In each question there are two switches Q1rad1 and Q2rad2 for answering True or False. How can I find out the value selected by each user when I click the submit button.

I understand that it may be more efficient when using Javascript, but for the purpose of this problem I have to use servlets.

+5
source share
4 answers

You must determine the value that you want to get when the switch is selected.

, , .

name , . , .

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

, , -

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)) {
       ...
    }
    ...

}
+8

. . , <input type="radio"> value. , onclick. </form> . :

 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>"
    );

doPost() , , request.getParameter(). - :

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

. .

Then in the request you will get a parameter with the name of the radio group and the selected value. After sending the servlet, the message recipient can use:

String value = request.getParameter("radioName");
+2
source

The lines below are sufficient for your HTML code.

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

For example, given your HTML code.

If Q1 is pressed

"TRUE"

then it will be our "enter" in the servlet.

+1
source

All Articles