Java 使用jsp的问答应用程序

Java 使用jsp的问答应用程序,java,html,jsp,Java,Html,Jsp,很抱歉问这个问题。在过去的几个小时里,我一直在寻找,但找不到任何帮助 我想使用JSP制作一个简单的测验应用程序。 我创建了两个表,如下所示 Col1:QuizId Col2:测验名称Col3:问题数量 第1列:数量第2列:问题第3列:选项1第4列:选项2第5列:选项3第6列:选项4第7列:更正选项 页面将显示如下图所示 此页的代码为 <%@page import="java.sql.*,java.io.*,java.text.*,java.util.* ,javax.serv

很抱歉问这个问题。在过去的几个小时里,我一直在寻找,但找不到任何帮助

我想使用JSP制作一个简单的测验应用程序。 我创建了两个表,如下所示

  • Col1:QuizId Col2:测验名称Col3:问题数量
  • 第1列:数量第2列:问题第3列:选项1第4列:选项2第5列:选项3第6列:选项4第7列:更正选项
  • 页面将显示如下图所示

    此页的代码为

    <%@page import="java.sql.*,java.io.*,java.text.*,java.util.*  ,javax.servlet.*,javax.servlet.http.*" 
     %> 
       <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
      <!DOCTYPE HTML>
        <HTML>
       <head>
      <meta charset="ISO-8859-1">
           <link rel="stylesheet" 
           href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" 
           integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" 
        crossorigin="anonymous">
        <link rel="stylesheet" href="bstyle.css" type="text/css">
        <title>Insert title here</title>
       </head>
         <body>
      <body>
      <%            
    String quiz_title =request.getParameter("quiz_title");
    String no_question =request.getParameter("no_question");
    String quiz_id=request.getParameter("quiz_id");
       %>
           <br><br>
          <div class="container">
    <div class="row">
        <div class="col-6">
            <h3><%=quiz_title %></h3>
        </div>
        <div class="col-6">
            <h3>Total no. of questions: <%=no_question %></h3>
        </div>
    </div>
    <br>
    <%
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student_portal 2.0","root","");
    Statement st=con.createStatement();
    String sql="select * from quiz_q_a where quiz_id="+quiz_id;
    ResultSet rs=st.executeQuery(sql);
    while(rs.next())
    {
        String question =rs.getString("question");
        String option_a =rs.getString("option_a");
        String option_b =rs.getString("option_b");
        String option_c =rs.getString("option_c");
        String option_d =rs.getString("option_d");
        String correct_option =rs.getString("correct_option");
        String explanation =rs.getString("explanation");
        String current_no =rs.getString("q_no");    
    %>
    <div class="row">
        <div class="col-6">
            <h4><%=current_no%>.  <%=question %></h4>
        </div>
        <div class="col-6">
            <h4>    &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;(&emsp;)
            </h4>
        </div>
    </div>  
    <br>
    <form>
        <div class="row">
            <div class="col-6">
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="inlineRadioOptions" value="A">
                  <label class="form-check-label" for="inlineRadio1">A. <%=option_a %></label>
                </div>
            </div>
            <div class="col-6">
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="inlineRadioOptions" value="B">
                  <label class="form-check-label" for="inlineRadio1">B. <%=option_b %></label>
                </div>
            </div>
        </div>
        <br>
        <div class="row">
            <div class="col-6">
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="inlineRadioOptions" value="C">
                  <label class="form-check-label" for="inlineRadio1">C. <%=option_c %></label>
                </div>
            </div>
            <div class="col-6">
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="inlineRadioOptions" value="D">
                  <label class="form-check-label" for="inlineRadio1">D. <%=option_d %></label>
                </div>
            </div>
        </div>
        <br>
        <hr>
    </form> 
    <%
    }
    %>  
       </div>
        <br>
    
         <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384- 
         DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"> 
       </script>
         <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" 
        integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" 
          crossorigin="anonymous"></script>
           <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" 
        integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" 
        crossorigin="anonymous"></script>
        
        
         </body>
          </html>
    
    
    在此处插入标题
    

    问题总数:
    . &emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp;(&emsp;)
    A. B
    C D


    我的要求是在页面末尾添加一个提交按钮。单击submit按钮时,用户响应应存储在数组中。应将从用户响应数组中选择的答案与表中的正确答案数组进行比较,并计算分数

    请帮助我实现这一目标。我无法捕获用户响应。谢谢。

    您需要将
    标记和
    提交
    按钮放在
    while loop
    之外。此外,由于单选按钮当前在您的表单中具有相同的值,因此您可以使用单选按钮的名称来唯一标识每个单选按钮,即:
    name=“inlineradioptions”
    。因此您的jsp代码如下所示:

     <form method="post" action="your_url">
         <%
        //your connection codes
        while(rs.next())
        {
           //other codes
              String current_no =rs.getString("q_no");    
        %>
        <div class="row">
            <div class="col-6">
                <h4><%=current_no%>.  <%=question %></h4>
            </div>
            <div class="col-6">
                <h4>    &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;(&emsp;)
                </h4>
            </div>
        </div>  
        <br>
        <form>
            <div class="row">
                <div class="col-6">
                    <div class="form-check">
                      <input class="form-check-input" type="radio" name="inlineRadioOptions_<%=current_no%>" value="A">
                      <label class="form-check-label" for="inlineRadio1">A. <%=option_a %></label>
                    </div>
                </div>
                <div class="col-6">
                    <div class="form-check">
                      <input class="form-check-input" type="radio" name="inlineRadioOptions_<%=current_no%>" value="B">
                      <label class="form-check-label" for="inlineRadio1">B. <%=option_b %></label>
                    </div>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-6">
                    <div class="form-check">
                      <input class="form-check-input" type="radio" name="inlineRadioOptions_<%=current_no%>"  value="C">
                      <label class="form-check-label" for="inlineRadio1">C. <%=option_c %></label>
                    </div>
                </div>
                <div class="col-6">
                    <div class="form-check">
                      <input class="form-check-input" type="radio" name="inlineRadioOptions_<%=current_no%>"  value="D">
                      <label class="form-check-label" for="inlineRadio1">D. <%=option_d %></label>
                    </div>
                </div>
            </div>
            <br>
            <hr>
        <%
        }
        %>  
    //will have value of quiz_id
      <input type="hidden" name-"quiz_id" value="<%=quiz_id%>">
        //added button
        <input type="submit">
        </form> 
    

    表单
    添加
    操作
    属性,以便存储/处理
    。这可能会有帮助(在您的情况下,接收者将是jsp/servlet)是的,我尝试过,但当我在表单中添加一个按钮时,该按钮会重复5次,因为我只需要在最后提交,一旦有人点击它,所有5个问题的答案都会认为您做错了什么,
    表单
    应汇总所有答案,以便随提交一起发送所有答案!(如我所见)就像我使用循环一样,我们需要在javascript上工作,并在某个地方存储结果,比如如果我做错了什么,请让我知道并帮助我谢谢你不需要javascript,通过表单将值发送到jsp,然后从那里处理得到了,夫人,非常感谢夫人理解了我做错的事情。再次感谢你的解释。
    <!--your connection codes-->
     //to get no coorect and wrong answer
    int wrong = 0, correct = 0;
    //getting quiz_id
    String quiz_id = request.getParameter("quiz_id");
    String sql = "select * from quiz_q_a where quiz_id=" + quiz_id;
    ResultSet rs = st.executeQuery(sql);
    while (rs.next()) {
     //get correct option for 1st ,2 ..etc
     String correct_option = rs.getString("correct_option");
     //get current question..
     String current_no = rs.getString("q_no");
     //get user answer
     String answers = request.getParameter("inlineRadioOptions_" + current_no);
     //check if equal
     if (answers.equals(correct_option)) {
      correct++; //increment
    
     } else {
    
      wrong++; //increment
     }
    }
    out.println("Correct Answer are" + correct);
    out.println("Wrong Answer are" + wrong);