I am currently coding a Quiz project in a JSP and have had problems with scorekeeping. All the answers and questions are stored in a database and retrieved with procedures, which work fine. My problem is that when the user presses the 'Next' button to go to the next question, some errors occur:
1) The radio button clicked that holds the right answer value does not seem to register with the code, and the radio buttons switch to the answers of the next question without comparing against the previously clicked radio buttons. 2) As a result, the score variable does not increment correctly. 3) Additionally, when going to succeeding questions, the score resets to 0, which is not what I want.
How do I fix these problems? Is there a way to do this without having to create a new page for each question and not have to use session attributes for the score?
My problems started when I added the code
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"));
}
Without the above code, the quiz works fine with the scoring but it only remains on question one even when the next button is clicked.
I am also aware that using scriptlets is not ideal, and will resolve this at a later date. For now I just want the code to work.
Here is the full code:
Connection conn = null;
ResultSet rs = null;
Statement st = null;
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"));
}
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mariadb://ebs-db.eastbarnetschool.com//DETAILS");
CallableStatement stmt = conn.prepareCall("{call GetQuestionTitle(?, ?)}");
stmt.setInt(1, QID);
stmt.registerOutParameter(2, Types.VARCHAR);
stmt.execute();
String description = stmt.getString(2);
%>
<%
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);
%>
<%
String chosenAnswer="";
if(request.getParameter("button")!=null)
{
chosenAnswer=request.getParameter("button").toString();
}
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);
%>
<%
if(request.getParameterValues("button") != null) {
if(chosenAnswer.equals(CorrectDescription)) {
score = score + 1;
%>
<h4 align="center"><font color="green" face="arial">You got Question <%=QID%> correct!</font></h4>
<% }
else{
%>
<h4 align="center"><font color="red" face="arial">You got Question <%=QID%> incorrect!</font></h4>
<%
}
}
%>
<br>
<form name="Quiz" method="post" action='Quiz.jsp'>
<br>
<center>
<table border="1" width="500px" bgcolor="lightblue" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<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>
<input type="submit" value="Next" name="submit">
</center></td></tr>
</table>
</td>
</tr>
</table>
</center>
<input name="QID" type="HIDDEN" value="<%=QID+1%>" id="thisField">
</form>
<%
} catch(Exception e){
System.out.println(e.getMessage());
} finally {
if (rs != null) rs.close();
if (st != null) st.close();
if (conn != null) conn.close();
}
out.println("Score = "+ score);
%>