当前位置: 首页 > 知识库问答 >
问题:

使用jsp的问答应用程序

国俊艾
2023-03-14

很抱歉问这个问题。我找了几个小时,但找不到任何帮助。

我想用JSP做一个简单的问答应用程序。我创建了如下2个表

  1. col1:QuizId col2:测验名称col3:问题数
  2. COL1:QtnNum COL2:问题COL3:option1COL4:option2COL5:option3COL6:option4COL7:correctoption

<%@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>

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

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

共有1个答案

秦彦君
2023-03-14

您需要将

标记和 submit按钮放在 while-loop之外。此外,由于单选按钮当前在表单中具有相同的值,因此为了唯一标识每个单选按钮,可以使用 current_no和单选按钮的名称,即: name=“inlineradiooptions_<%=current_no%>”。因此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> 

然后,在服务器端,您需要再次使用selectquery根据quiz_id获取结果,并根据这个增量值检查用户answer和correct_answer是否相同。即:

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

 类似资料:
  • 我的Spring启动Web应用程序有问题。它使用嵌入式jetty(必须是jetty-而不是tomcat),并且使用jsp视图。但似乎没有办法让嵌入式jetty与jsp视图一起工作。实际上,当我运行mu应用程序时,服务器启动成功,但当我尝试访问我的Web应用程序的任何url时,我收到错误消息: 我如何能使它工作?有什么办法吗?如果用spring boot是不可能的,那么有什么替代方法可以让我的应用在

  • 当我已经使用JSP作为登录/注册表单时,我在Spring MVC应用程序中使用vaadin。我有问题,可能WebSecurity没有为我的vaadin视图提供POST方法。我想在成功验证用户后返回一个组件的vaadin视图。我不知道问题出在哪里。 将JSP与vaadin集成可能很奇怪,但我认为这很简单 配置: 控制器: 瓦丁视图: 属性: pom.xml 当我打开“localhost:8080/u

  • 我是Spring Framework的新手。试图制作一个基于Java的Spring MVC项目。这是我的主要应用程序类 当我运行项目时,我得到了错误 为什么Spring不能显示JSP文件?

  • 我的控制器 我的主营 我还补充了

  • 亲爱的,我已经部署了如下示例服务: 当我执行get nodes-o wide时,如下所示, 任何帮助。谢了。