2016-07-04 110 views
0

我有下面的代碼,一切工程找到,直到我嘗試重定向回登錄頁面,我得到以下錯誤:非法狀態異常。JSP重定向非法狀態異常

我讀過這可能是因爲響應已被提交,但我看不到在哪裏。任何幫助表示讚賞。

<div id="questionContainer"> 
    <form method="POST"> 
     <% 
     for (int ques = 1; ques < 56; ques++) { 
      try { 
       if (iterate.hasNext()) { 
        String[] row = (String[]) iterate.next(); 
        if (row[0].equals("1")) { 
         out.println("<div class='trigger' name='trigger" + ques + "'>"); 
         out.println("<h2 class='page-header'>Question " + ques + "</h2>"); 
         out.println("<p id='question'>" + row[1] + "</p><br/>"); 
         out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='A' checked>" + row[2] + "</p>"); 
         out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='B' >" + row[3] + "</p>"); 
         out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='C' >" + row[4] + "</p>"); 
         out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='D' >" + row[5] + "</p><br/>"); 
         out.println("</div>"); 
        } else if (row[0].equals("2")) { 
         out.println("<div class='trigger' name='trigger" + ques + "'>"); 
         out.println("<h2 class='page-header'>Question " + ques + "</h2>"); 
         out.println("<p id='question'>" + row[1] + "</p><br/>"); 
         out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='A'checked>" + row[2] + "</p>"); 
         out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='B'>" + row[3] + "</p>"); 
         out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='C'>" + row[4] + "</p>"); 
         out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='D'>" + row[5] + "</p>"); 
         out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='E'>" + row[6] + "</p><br/>"); 
         out.println("</div>"); 
        } else if (row[0].equals("3")) { 
         out.println("<div class='trigger' name='trigger" + ques + "'>"); 
         out.println("<h2 class='page-header'>Question " + ques + "</h2>"); 
         out.println("<p id='question'>" + row[1] + "</p><br/>"); 
         out.println("Answer: <p id='ansA'><input type='text' name='answer" + ques + "'</p><br/><br/>"); 
         out.println("</div>"); 
        } 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     %> 
    </form> 

    <button type="button" id="Next" name="Next">Next</button> <button type="submit" value="submited" class="submitBtn" id="Submit" name="Submit">Submit</button> 

</div> 


<script> 

    var $curCount = 1; 

    $(document.getElementsByName("trigger" + $curCount)).show(); 

    $(document.getElementById("Next")).click(function() { 
     $(document.getElementsByName("trigger" + $curCount)).hide(); 
     $curCount++; 
     $(document.getElementsByName("trigger" + $curCount)).show(); 
     if ($curCount > 55) 
     { 
      //$(document.getElementsByName("trigger" + $curCount)).hide(); 
      $curCount = 1; 
      $(document.getElementsByName("trigger" + $curCount)).show(); 
      //window.alert("in loop" + $curCount); 

      $(document.getElementsByName("Submit")).show(); 

     } 
    }); 


    <% if (request.getParameter("Submit") != null && request.getParameter("Submit").equals("submited")) { 

      ArrayList listAnswers = new ArrayList(); 
      for (int ques = 1; ques < 56; ques++) { 

       String s = request.getParameter("answer" + ques); 
       listAnswers.add(s); 
      } 
      con.writePAQuestions("test", listAnswers); 
      response.sendRedirect("main.jsp"); 

     } 

    %> 
</script> 

回答

0

我仍然不知道爲什麼這會導致錯誤。不過我繞過它在jsp標籤中使用以下內容:

out.println("window.location='login.jsp';");