我正在尝试进行测验,我的查询是 ORDER BY RAND() 所以这意味着它将是随机的...我的 JSP 将它发送到 servlet 以处理流程但我遇到了问题,我已经存储了 4 个问题通过数据库中的选择(答案)和下面的代码,我可以检索它。问题是,当我检索考生的答案时,唯一检索到的数据是第一个答案......见我的 servlet
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="database.Connect" %>
<%
Connect conn = new Connect();
Statement stmt = conn.getDataConn().createStatement();
ResultSet rs = stmt.executeQuery("SELECT * from quiz WHERE category = 'secondary' ORDER BY RAND()");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="../../css&js/des.css">
<script src="css&js/modernizr.custom.js"></script>
<title>Question 1</title>
</head>
<body>
<img alt="" src="../../pics/bgimg2.jpg" id="bgimg" />
<div class="md-modal md-effect-1 md-show" id="modal-1">
<form action="../../Check" method="POST">
<table border="1" class="c-form md-content">
<% while(rs.next()){
%>
<tr>
<td><h1><%=rs.getString("question")%>?</h1></td>
<td><select name="answer">
<option value="<%=rs.getString("choice1")%>"><%=rs.getString("choice1")%></option>
<option value="<%=rs.getString("choice2")%>"><%=rs.getString("choice2")%></option>
<input type="hidden" name="correct" value="<%=rs.getString("correct")%>"/>
</select></td>
</tr>
<%}
%>
<tr>
<td align="center" colspan="2"><input type="submit" value="Next"/></td>
</tr>
</table>
</form>
</div>
</body>
我正在尝试进行测验,我的查询是 ORDER BY RAND() 所以这意味着它将是随机的...我的 JSP 将它发送到 servlet 以处理流程但我遇到了问题,我已经存储了 4 个问题通过数据库中的选择(答案)和下面的代码,我可以检索它。问题是,当我检索考生的答案时,唯一检索到的数据是第一个答案......见我的 servlet
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="database.Connect" %>
<%
Connect conn = new Connect();
Statement stmt = conn.getDataConn().createStatement();
ResultSet rs = stmt.executeQuery("SELECT * from quiz WHERE category = 'secondary' ORDER BY RAND()");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="../../css&js/des.css">
<script src="css&js/modernizr.custom.js"></script>
<title>Question 1</title>
</head>
<body>
<img alt="" src="../../pics/bgimg2.jpg" id="bgimg" />
<div class="md-modal md-effect-1 md-show" id="modal-1">
<form action="../../Check" method="POST">
<table border="1" class="c-form md-content">
<% while(rs.next()){
%>
<tr>
<td><h1><%=rs.getString("question")%>?</h1></td>
<td><select name="answer">
<option value="<%=rs.getString("choice1")%>"><%=rs.getString("choice1")%></option>
<option value="<%=rs.getString("choice2")%>"><%=rs.getString("choice2")%></option>
<input type="hidden" name="correct" value="<%=rs.getString("correct")%>"/>
</select></td>
</tr>
<%}
%>
<tr>
<td align="center" colspan="2"><input type="submit" value="Next"/></td>
</tr>
</table>
</form>
</div>
</body>
Servlet ...我只是想在这里进行实验,但我真的需要帮助.. for 的输出为空,并且过去的代码也失败了,因为第一个答案是唯一的检索,我需要增加它。请帮忙
package Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Check extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
for(int i=0;i<10;i++) {
String Answer = request.getParameter("answer" + i);
System.out.println(Answer);
}
String Answer = request.getParameter("answer");
String Correct = request.getParameter("correct");
int score = 0;
if (Answer.equals(Correct)) {
score++;
}
System.out.println(score);
}
}