Java 单击“下一步”按钮时,JSP测验不会转到下一个问题

Java 单击“下一步”按钮时,JSP测验不会转到下一个问题,java,html,forms,jsp,button,Java,Html,Forms,Jsp,Button,我正在JSP中构建一个测验,每次显示一个问题和四个答案,所有答案都来自MySQL数据库。目前,使用下面的代码,页面能够显示第一个问题,并允许用户通过单选按钮提交答案。但是,单击“下一步”按钮后,页面将刷新,仍然显示问题1。我想让它显示下一个问题,第二个问题,第三个问题,依此类推。我该怎么做 如果可能的话,有人能为我写代码而不仅仅是解释它吗 此外,我非常清楚使用scriptlets不是一件好事,但是,我还远远不够先进,不足以胜任其他工作。很抱歉代码太乱了,我不得不匆忙完成这个项目,因为我只有一周的

我正在JSP中构建一个测验,每次显示一个问题和四个答案,所有答案都来自MySQL数据库。目前,使用下面的代码,页面能够显示第一个问题,并允许用户通过单选按钮提交答案。但是,单击“下一步”按钮后,页面将刷新,仍然显示问题1。我想让它显示下一个问题,第二个问题,第三个问题,依此类推。我该怎么做

如果可能的话,有人能为我写代码而不仅仅是解释它吗

此外,我非常清楚使用scriptlets不是一件好事,但是,我还远远不够先进,不足以胜任其他工作。很抱歉代码太乱了,我不得不匆忙完成这个项目,因为我只有一周的时间来完成它

Connection conn = null;
ResultSet rs = null;
Statement st = null;

String action;
Scanner input = new Scanner(System.in);

int score = 0;
int count = 0;
int QID = 1;


try {
    Class.forName("org.mariadb.jdbc.Driver");
    System.out.println("Connecting to a selected database...");
    conn = DriverManager.getConnection(
            "jdbc:mariadb://ebs-db.eastbarnetschool.com//DETAILS);
    System.out.println("Connection made");


    CallableStatement stmt = conn.prepareCall("{call GetQuestionTitle(?, ?)}");
        stmt.setInt(1, QID); 
        stmt.registerOutParameter(2, Types.VARCHAR);
        stmt.execute();
        String description = stmt.getString(2);
        System.out.println(description);
%>
<% 

    CallableStatement answer1 = conn.prepareCall("{call GetAnswer1(?, ?)}");
        answer1.setInt(1, QID); 
        answer1.registerOutParameter(2, Types.VARCHAR);
        answer1.execute();
        String answerOne = answer1.getString(2);
        System.out.println(answerOne);

    CallableStatement answer2 = conn.prepareCall("{call GetAnswer2(?, ?)}");
        answer2.setInt(1, QID); 
        answer2.registerOutParameter(2, Types.VARCHAR);
        answer2.execute();
        String answerTwo = answer2.getString(2);
        System.out.println(answerTwo);

    CallableStatement answer3 = conn.prepareCall("{call GetAnswer3(?, ?)}");
        answer3.setInt(1, QID); 
        answer3.registerOutParameter(2, Types.VARCHAR);
        answer3.execute();
        String answerThree = answer3.getString(2);
        System.out.println(answerThree);

    CallableStatement answer4 = conn.prepareCall("{call GetAnswer4(?, ?)}");
        answer4.setInt(1, QID); 
        answer4.registerOutParameter(2, Types.VARCHAR);
        answer4.execute();
        String answerFour = answer4.getString(2);
        System.out.println(answerFour);

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

%> 
<br>
<form name="Quiz" method="get" 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="next">

</center></td></tr> 
</table>    
</td>
</tr>
</table>
</center>

<%
if(request.getParameterValues("button") != null) {
       if(chosenAnswer.equals(CorrectDescription)) {
           score = score + 1;
%>
<h4 align="center"><font color="green" face="arial">You got Question <%=QID-1%> correct!</font></h4>
<%
}   
       else{
%>
<h4 align="center"><font color="red" face="arial">You got Question <%=QID-1%> incorrect!</font></h4>
<%  
    }   
    }
%>
<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();
}

连接连接=null;
结果集rs=null;
语句st=null;
弦作用;
扫描仪输入=新扫描仪(System.in);
智力得分=0;
整数计数=0;
int-QID=1;
试一试{
Class.forName(“org.mariadb.jdbc.Driver”);
System.out.println(“连接到选定的数据库…”);
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);
系统输出打印项次(说明);
%>


测验 问题: 1: 2: 三: 4: 你的问题对了! 你的问题不对! 我假设(我自己不做任何JSP)

  • 删除
    ONCLICK=>
    ,因为它没有任何用处

  • int QID=1
    更改为


  • 这将让

    <input name="QID" type="HIDDEN" value="<%=QID+1%>" id="thisField">
    
    
    
    做好这项工作


    您将需要代码来避免检查可用问题的数量

    什么是
    ONCLICK=>
    应该做什么?看起来您在这里混合了服务器端脚本和客户端脚本的想法哦,这是一段多余的代码,我忘了删除,谢谢。如果上面的代码不正确,您每次调用代码时都会设置
    int QID=1;
    quick.jsp
    那么你是在建议循环吗?我将QID设置为1,因为这是测验的起点。你需要从请求中读取QID,并且仅当它是第一次进入代码(第一次加载页面)时才进行设置。虽然这样做有效,但我现在确实有一些单独的问题,但谢谢你。
    <input name="QID" type="HIDDEN" value="<%=QID+1%>" id="thisField">