简体   繁体   English

Java Servlet:如何检索选定的单选按钮值?

[英]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.我创建了一个简单的 servlet,其中将向用户提出 2 个问题,回答真或假。 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.每个问题都有 2 个单选按钮,Q1rad1 和 Q2rad2,用于回答 True 或 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.我知道使用 Javascript 可能更有效,但出于这个问题的目的,我必须使用 servlets。

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.当您按下 select 一个按钮时,同一组中的所有其他按钮均未选中。

<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在您将收到请求的 Servlet 中,您将拥有类似的东西

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

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"> .此外,每个<input type="radio">都应该有一个value属性。 I'm not sure you need the onclick handler at all.我不确定您是否需要onclick处理程序。 You should also have a </form> closer tag.您还应该有一个</form>更接近的标签。 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() .然后在处理表单提交的 servlet 的doPost()方法中,您可以使用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...
}

Give the same name to the radios of the same question, and set different values.给同一个问题的radio起相同的名字,设置不同的值。 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:提交 servlet 后接收帖子可以使用:

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

For your HTML Code the below lines are enough对于您的 HTML 代码,以下几行就足够了

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

For example, Considering your HTML Code.例如,考虑您的 HTML 代码。

If Q1 is pressed如果按下 Q1

"TRUE" “真的”

then it would be our "Input" in Servlet.那么它将是我们在 Servlet 中的“输入”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM