【问题标题】:JSP Quiz not going to the next question on click of Next button [closed]单击“下一步”按钮时,JSP 测验不会转到下一个问题 [关闭]
【发布时间】:2020-05-25 14:47:05
【问题描述】:

我正在 JSP 中构建一个测验,一次显示一个问题,有四个答案,所有这些答案都是从 MySQL 数据库中获得的。目前,使用以下代码,页面能够显示第一个问题并允许用户通过单选按钮提交答案。但是,单击“下一步”按钮时,页面会刷新以仍显示问题一。我希望它显示下一个问题,所以第二个问题,然后是第三个问题,依此类推。我该怎么做?

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

另外,我非常清楚使用 scriptlet 不是一件好事,但是,我还远远不够先进,无法做到这一点。抱歉代码乱七八糟,因为我只有一周的时间来完成它,所以我不得不匆忙完成这个项目。

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();
}

【问题讨论】:

  • ONCLICK = &lt;%=QID=QID+1%&gt;&gt; 应该做什么?看起来你在这里混合了服务器和客户端脚本的想法
  • 哦,那是一段多余的代码,我忘记删除了,谢谢。
  • 如果上面的代码是Quiz.jsp,则每次调用代码时都设置int QID = 1;
  • 那么您是在建议循环吗?我将 QID 设置为 1,因为这是测验的起点。
  • 您需要从请求中读取 QID - 并且只有在第一次进入代码时才设置它(第一次加载页面)

标签: java html forms jsp button


【解决方案1】:

我假设(我自己不做任何 JSP)

  1. 删除ONCLICK = &lt;%=QID=QID+1%&gt;&gt;,因为它没有任何用处

  2. int QID=1更改为


int QID=1; 
if(request.getParameter("QID")!=null) {   
  QID=Integer.parseInt(request.getParameter("QID"));
}

这会让

<input name="QID" type="HIDDEN" value="<%=QID+1%>" id="thisField">

做好工作

您将需要代码来不超过可用问题的数量

【讨论】:

  • 虽然这行得通,但我现在确实有一些单独的问题,但谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-26
  • 2013-05-24
  • 2021-02-17
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2018-11-04
相关资源
最近更新 更多