如何在尝试通过 Servlet 和 JSP 进行的测验时获取用户选择的选项



我正在尝试通过Servlets和Jsp开发一个在线测验。 我正在使用 MYSQl 作为后端数据库。 要求是向用户显示几个随机问题。 我必须将测验结果存储在单独的表中。 该表应包含详细信息,例如用户名,向每个用户提出的问题,是否正确

目前,我可以在单个网页上显示所有问题。

但是我在尝试测验时无法获取用户选择的答案。 我需要存储用户选择的每个选项并将其传输到 JSP 页面。

以下是代码:-

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String url = "jdbc:mysql://127.0.0.1:3306/Quiz";
String username1="root";
String password1="root";
PrintWriter out= response.getWriter();
PreparedStatement st = null;
ResultSet results = null;
int limit=3;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection(url,username1,password1);
String sql="SELECT  Questions.Question, Questions.option1, Questions.option2,Questions.option3, Questions.option4,Questions.correctanswer FROM Questions order by RAND() limit ?";
st = (PreparedStatement) con.prepareStatement(sql);
st.setInt(1,limit);
results = st.executeQuery();
int i=1;
while (results.next())
{
String r1, r2, r3, r4, r5,r6;
r1 = results.getString(1);
r2 = results.getString(2);
r3 = results.getString(3);
r4 = results.getString(4);
r5 = results.getString(5);
r6 = results.getString(6);
out.println("<BODY BGCOLOR='powderblue'></BODY>" +
"<FORM method="post" action= "Results.jsp" >" +
"<TABLE BORDER = 2 CELLPADDING = 4 CELLSPACING = 2>" +
"<TR><TD COLSPAN = 6 ALIGN = CENTER><H2><B>Quick Quiz</B></H2></TD></TR>");
out.println("<TR><TD>" + r1 + "</TD></TR>");
out.println("<TR><TD><input type = "radio" name = "answer" id="input1"  value = "1"  required> " + r2 + "</TD></TR><BR>");
out.println("<TR><TD><input type = "radio" name = "answer" id="input2" value = "2">" + r3 + "</TD></TR><BR>");
out.println("<TR><TD><input type = "radio" name = "answer" id="input3" value = "3">" + r4 + "</TD></TR><BR>");
out.println("<TR><TD><input type = "radio" name = "answer" id="input4" value = "4">" + r5 + "</TD></TR><BR>");
out.println("</TABLE>");
session.setAttribute("question"+i,r1);
session.setAttribute("correctanswer"+i, r6);
if (i <3)
{
out.println("</FORM></BODY></HTML>");
}
else
{
out.println("<BR><BR><INPUT type = "submit" value = "Press to Submit"></form></body></html>" );
}   
i++;
}
// session.setAttribute("selectedanswer1",(String)(request.getParameter("answer")) );  ---not working as expected          
results.close();
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 

} 

请您帮助我获取测验结果。

我还试图在单独的网页中显示每个问题,并提供"下一步"按钮选项以移动到下一个问题,但我无法做到这一点。 所以我尝试了其他方式,并在一页上给出了所有问题.但是我在尝试测验时无法获取用户选择的选项。

我使用的必需选项也无法正常工作。

请帮助我获取用户选择的答案。

谢谢

您的表单布局不正确。

如果我误解了,请纠正我,但是您的servlet生成的html应该看起来像这样吗?

<BODY BGCOLOR='powderblue'></BODY>
<FORM method="post" action= "Results.jsp" >
<TABLE BORDER = 2 CELLPADDING = 4 CELLSPACING = 2>
<TR>
<TD COLSPAN = 6 ALIGN = CENTER>
<H2><B>Quick Quiz</B></H2>
</TD>
</TR>
<TR>
<TD></TD>
</TR>
<TR>
<TD><input type = "radio" name = "answer" id="input1"  value = "1"  required></TD>
</TR>
<BR>
<TR>
<TD><input type = "radio" name = "answer" id="input2" value = "2"></TD>
</TR>
<BR>
<TR>
<TD><input type = "radio" name = "answer" id="input3" value = "3"></TD>
</TR>
<BR>
<TR>
<TD><input type = "radio" name = "answer" id="input4" value = "4"></TD>
</TR>
<BR>
</TABLE>
</FORM>
</BODY>
<BODY BGCOLOR='powderblue'></BODY>
<FORM method="post" action= "Results.jsp" >
<TABLE BORDER = 2 CELLPADDING = 4 CELLSPACING = 2>
<TR>
<TD COLSPAN = 6 ALIGN = CENTER>
<H2><B>Quick Quiz</B></H2>
</TD>
</TR>
<TR>
<TD></TD>
</TR>
<TR>
<TD><input type = "radio" name = "answer" id="input1"  value = "1"  required></TD>
</TR>
<BR>
<TR>
<TD><input type = "radio" name = "answer" id="input2" value = "2"></TD>
</TR>
<BR>
<TR>
<TD><input type = "radio" name = "answer" id="input3" value = "3"></TD>
</TR>
<BR>
<TR>
<TD><input type = "radio" name = "answer" id="input4" value = "4"></TD>
</TR>
<BR>
</TABLE>
</FORM>
</BODY>
<BODY BGCOLOR='powderblue'></BODY>
<FORM method="post" action= "Results.jsp" >
<TABLE BORDER = 2 CELLPADDING = 4 CELLSPACING = 2>
<TR>
<TD COLSPAN = 6 ALIGN = CENTER>
<H2><B>Quick Quiz</B></H2>
</TD>
</TR>
<TR>
<TD></TD>
</TR>
<TR>
<TD><input type = "radio" name = "answer" id="input1"  value = "1"  required></TD>
</TR>
<BR>
<TR>
<TD><input type = "radio" name = "answer" id="input2" value = "2"></TD>
</TR>
<BR>
<TR>
<TD><input type = "radio" name = "answer" id="input3" value = "3"></TD>
</TR>
<BR>
<TR>
<TD><input type = "radio" name = "answer" id="input4" value = "4"></TD>
</TR>
<BR>
</TABLE>
<BR><BR><INPUT type = "submit" value = "Press to Submit">
</form>
</body></html>

让我们在这里逐步浏览您的 html。

出于某种原因,你不止一次地创建身体,然后自我关闭它。

您有多个表单,甚至有多个单独的 HTML 元素,而在任何有效的 HTML 文档中,都应该只有一个。

典型的表单应如下所示

<html>
	<body>
		<form method='post' action='Results.jsp'>
		<fieldset>
			<legend>What's the capitol of the United States?</legend>
			<input type="radio" name="question1_answer" value="1" id="question1_answer1">
			<label for="question1_answer1">Pennsylvania</label>
		
			<input type="radio" name="question1_answer" value="2" id="question1_answer2">
			<label for="question1_answer2">New York</label>
		
			<input type="radio" name="question1_answer" value="3" id="question1_answer3">
			<label for="question1_answer3">Washington D.C.</label>
		
			<input type="radio" name="question1_answer4" value="4" id="question1_answer4">
			<label for="question1_answer4">Texas</label>
		</fieldset>
		<br>
		<fieldset>
			<legend>What's 10 divided by 2</legend>
			<input type="radio" name="question2_answer" value="1" id="question2_answer1">
			<label for="question2_answer1">5</label>
		
			<input type="radio" name="question2_answer" value="2" id="question2_answer2">
			<label for="question2_answer2">3</label>
		
			<input type="radio" name="question2_answer" value="3" id="question2_answer3">
			<label for="question2_answer3">12</label>
		
			<input type="radio" name="question2_answer4" value="4" id="question2_answer4">
			<label for="question2_answer4">8</label>
		</fieldset>
		<br>
		<input type="submit" value="Press to Submit">
		</form>
	</body>
</html>

然后,在你的 servlet 中,你会做这样的事情来得到答案。

for(int i = 0; i < limit; i++){
String answer = request.getAttribute("question"+i+"_answer");
}

编辑:Servlet Code可能看起来像这样,大致如此。

String url = "jdbc:mysql://127.0.0.1:3306/Quiz";
String username1="root";
String password1="root";
PrintWriter out= response.getWriter();
PreparedStatement st = null;
ResultSet results = null;
int limit=3;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection(url,username1,password1);
String sql="SELECT  Questions.Question, Questions.option1, Questions.option2,Questions.option3, Questions.option4,Questions.correctanswer FROM Questions order by RAND() limit ?";
st = (PreparedStatement) con.prepareStatement(sql);
st.setInt(1,limit);
results = st.executeQuery();
int questionnumber = 0;
out.println("<html><body><form method='post' action='Results.jsp'>");
while (results.next())
{
questionnumber++;
String question = results.getString(1);
HashMap<Integer, String> questions = new HashMap<Integer, String>();
questions.put(1, results.getString(2));
questions.put(2, results.getString(3));
questions.put(3, results.getString(4));
questions.put(4, results.getString(5));
foreach(Map.Entry<Integer, String> entry : questions.entrySet()){
String answertext = entry.getValue();
Integer answernumber = entry.getKey();
out.println("<input type='radio' name='question" + questionnumber + "_answer' value='" + answernumber + "' id='question" + questionnumber + "_answer" + answernumber + "'>");
out.println("<label for='question" + questionnumber + "_answer" + answernumber + "'>" + answertext + "</label>")
}
out.println("<fieldset><legend>" + question + "</legend>");
out.println("</fieldset>");
}
out.println("<input type='submit' value='Press to Submit'></form></body></html>");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 

最新更新