使用JSP进行测验应用

教育4乐趣:

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

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

  1. Col1:QuizId Col2:测验名称Col3:问题数
  2. Col1:QtnNum Col2:问题Col3:Option1 Col4:Option2 Col5:Option3 Col6:Option4 Col7: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/[email protected]/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>

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

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

斯瓦蒂语:

您还需要将<form>标签和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> 

然后,在服务器端,您需要根据再次使用select查询来获取结果,quiz_id并根据此增量值来检查用户的答案和正确的答案是否相同。即:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章