Java 所选单选按钮获取上一个单选按钮的值
我正在用JSP编写一个测验,但单选按钮似乎无法正常工作。这些问题存储在MySQL数据库中,并使用过程进行检索。当用户单击单选按钮并提交时,即使选择了正确的答案,代码也将始终输出“不正确”。在运行代码时,我注意到单选按钮似乎跳回到上一个问题的答案 例如,对于问题1,所选单选按钮不返回任何内容。对于问题2,单选按钮返回问题1的答案。对于问题3,它返回与问题2相同位置的答案,依此类推 我该如何解决这个问题 我知道使用Scriptlet是一种不好的做法;这将在我完成代码开发后解决Java 所选单选按钮获取上一个单选按钮的值,java,html,jsp,Java,Html,Jsp,我正在用JSP编写一个测验,但单选按钮似乎无法正常工作。这些问题存储在MySQL数据库中,并使用过程进行检索。当用户单击单选按钮并提交时,即使选择了正确的答案,代码也将始终输出“不正确”。在运行代码时,我注意到单选按钮似乎跳回到上一个问题的答案 例如,对于问题1,所选单选按钮不返回任何内容。对于问题2,单选按钮返回问题1的答案。对于问题3,它返回与问题2相同位置的答案,依此类推 我该如何解决这个问题 我知道使用Scriptlet是一种不好的做法;这将在我完成代码开发后解决 <% //In
<%
//Initalising necessary variables to be used in connection stream
Connection conn = null;
ResultSet rs = null;
Statement st = null;
//Setting SCORE and QID to 0
//Checking to see if SCORE and QID already exist
//If so, the existing value is retrieved and overwritten
//The variables now hold the existing values
int score = 0;
if(request.getParameter("score")!=null) {
score=Integer.parseInt(request.getParameter("score"));
}
int QID=1;
if(request.getParameter("QID")!=null) {
QID=Integer.parseInt(request.getParameter("QID"));
}
//Setting up connection stream to MySQL database on the server
//Using my credentials to log in and access tables
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mariadb://URL;
//Calling procedure to obtain question description from the stored question table in stored database
CallableStatement stmt = conn.prepareCall("{call GetQuestionTitle(?, ?)}");
stmt.setInt(1, QID);
stmt.registerOutParameter(2, Types.VARCHAR);
stmt.execute();
String description = stmt.getString(2);
%>
<%
//Calling all procedures to obtain the answers to each question stored in the database
CallableStatement answer1 = conn.prepareCall("{call GetAnswer1(?, ?)}");
answer1.setInt(1, QID);
answer1.registerOutParameter(2, Types.VARCHAR);
answer1.execute();
String answerOne = answer1.getString(2);
CallableStatement answer2 = conn.prepareCall("{call GetAnswer2(?, ?)}");
answer2.setInt(1, QID);
answer2.registerOutParameter(2, Types.VARCHAR);
answer2.execute();
String answerTwo = answer2.getString(2);
CallableStatement answer3 = conn.prepareCall("{call GetAnswer3(?, ?)}");
answer3.setInt(1, QID);
answer3.registerOutParameter(2, Types.VARCHAR);
answer3.execute();
String answerThree = answer3.getString(2);
CallableStatement answer4 = conn.prepareCall("{call GetAnswer4(?, ?)}");
answer4.setInt(1, QID);
answer4.registerOutParameter(2, Types.VARCHAR);
answer4.execute();
String answerFour = answer4.getString(2);
%>
<%
//Defining String variable CHOSENANSWER to store value of user-selected radio button
String chosenAnswer="";
if(request.getParameter("button")!=null)
{
chosenAnswer=request.getParameter("button").toString();
}
//Calling procedure to obtain the CorrectAnswer from database
CallableStatement stmt2 = conn.prepareCall("{call GetCorrectAnswer(?, ?)}");
stmt2.setInt(1, QID);
stmt2.registerOutParameter(2, Types.VARCHAR);
stmt2.execute();
String CorrectDescription = stmt2.getString(2);
System.out.println("\nCorrect Answer: " + CorrectDescription);
%>
<%
//For all questions up to Question 20 and not beyond:
if (QID < 21){
%>
<!--- QUIZ GUI --->
<!--- Creating a submit form for the radio buttons --->
<!--- Page refreshes on submit to the next question --->
<br>
<form name="Quiz" method="post" action='Quiz.jsp'>
<br>
<!--- Using a table for neater alignment with the questions and answers --->
<center>
<table border="1" width="500px" bgcolor="lightblue" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<!--- Setting headings and radio buttons --->
<!--- Using properties to adjust text colour, font and size --->
<!--- The variables that contain the descriptions are passed in using Java scriptlets --->
<h1 align="center"><font color="white" face="arial">Quiz</font></h1>
<table border="0" width="500px" cellspacing="2" cellpadding="6">
<tr>
<td width="50%"><font color="steelblue" face="arial" size=4><span style="font-weight:normal"> QUESTION <%=QID%></span></font></td>
<tr>
<td width="100%"><font color="black" face="arial" size=4><span style="font-weight:normal"><%=description%></span></font></td></tr>
<tr>
<td>
1: <input type="radio" name="button" value= "<%=answerOne%>" /><font face="arial" size=3><%=answerOne%></font></td>
<tr>
<td>
2: <input type="radio" name="button" value="<%=answerTwo%>" /><font face="arial" size=3><%=answerTwo%></font></td>
<tr>
<td>
3: <input type="radio" name="button" value="<%=answerThree%>" /><font face="arial" size=3><%=answerThree%></font></td>
<tr>
<td>
4: <input type="radio" name="button" value="<%=answerFour%>" /><font face="arial" size=3><%=answerFour%></font></td>
<tr><td><center>
<!--- Submit button to go to next question --->
<input type="submit" value="Next" name="submit">
<!--- If the submit button is pressed: --->
<%
//Check to see if selected radio button matches CorrectAnswer procedure value
if(chosenAnswer.equals(CorrectDescription)) {
%>
<!--- Increase SCORE by 1 and output correct if answer matches correct answer --->
<input name="score" type="HIDDEN" value="<%=score+1%>" id="scoreField">
<h4 align="center"><font color="green" face="arial">Correct!</font></h4>
<%
}
else{
%>
<!--- Otherwise SCORE remains constant and an incorrect message is output --->
<input name="score" type="HIDDEN" value="<%=score%>" id="scoreField">
<h4 align="center"><font color="red" face="arial">Incorrect!</font></h4>
<%
}
System.out.println(chosenAnswer);
%>
<%
//Passing score as a session attribute so that it remains as a variable on page refresh and redirect
//session.setAttribute("Score", score);
%>
</center></td></tr>
</table>
</td>
</tr>
</table>
</center>
<!--- Increment QID by 1 to go to the next question after submit button pressed --->
<input name="QID" type="HIDDEN" value="<%=QID+1%>" id="thisField">
</form>
当您的页面第一次加载时,您的QID
是1
,然后您增加它,即:“
,当您提交QID
时,它变成2
,因此我认为问题在于CallableStatement stmt2=conn.prepareCall({call GetCorrectAnswer(?)})59
<input type="hidden" name="current_question" value="<%=QID%>"/>
int previous_question_no;
String chosenAnswer="";
if(request.getParameter("button")!=null)
{
chosenAnswer=request.getParameter("button").toString();
//getting question no which is submitted
previous_question_no=Integer.parseInt(request.getParameter("current_question"));
}
//Calling procedure to obtain the CorrectAnswer from database
CallableStatement stmt2 = conn.prepareCall("{call GetCorrectAnswer(?, ?)}");
//passing same question no
stmt2.setInt(1, previous_question_no);
stmt2.registerOutParameter(2, Types.VARCHAR);
..