jsp,Servlet仅在提交页面时重新加载表单

时间:2016-03-26 07:25:41

标签: java jsp servlets

我正在做的项目基于Java jsp和Servlets

我正在创建一个问题导航器,一旦点击提交按钮就会导航到一个新问题。下面显示的是我在jsp页面中使用的表单。

<form name="form1" id="form1" method="POST" action="${pageContext.request.contextPath}/QuestionGenerateUser"> 
                            <div class="form-group" id="question">
                                <div class="labelalign">
                                    <label class="radio1" for="answer1">${Question}?</label>
                                    <input type="text" value="${QuestionID}" name="id" id="id" style="visibility: hidden;"/>
                                </div>
                                <label class="labels" ><input type="radio" name="answer" id="answer" value="1" checked="checked">${answer1}</label>
                                <span style="visibility: hidden;" class="error2" id="ans"></span>

                                <span style="visibility: hidden;" class="error2" id="answ1"></span><br>
                                <label class="labels" ><input type="radio" name="answer" id="answer" value="2">${answer2}</label>


                                <span style="visibility: hidden;" class="error2" id="answ2"></span><br>
                                <label class="labels" ><input type="radio" name="answer" id="answer" value="3">${answer3}</label>


                                <span style="visibility: hidden;" class="error2" id="answ3"></span><br>
                                <label class="labels" ><input type="radio" name="answer" id="answer" value="4">${answer4}</label>


                                <span style="visibility: hidden;" class="error2" id="answ4"></span><br>
                            </div>

                            <div style="margin-top: 30px; float: right;" >

                                <input class="btn btn-success" type="submit" name="submitAnswer" id="submit" value="Next Question" onclick="javascript:callMyMethodToSubmitAjax()"/>

                            </div>
                        </form> 

这是我的servlet代码,它从类中获取数据并设置为jsp

if (request.getParameter("submitAnswer") != null) {
            PrintWriter out = response.getWriter();

            String id = request.getParameter("id");
            String answer = request.getParameter("answer");
            Answer anAnswer = new Answer(id, answer);

            questionProcessor.addAnswer(anAnswer);
            Question question = questionProcessor.getQuestions();
            request.setAttribute("Question", question.getQuestion());
            request.setAttribute("answer1", question.getAnswer1());
            request.setAttribute("answer2", question.getAnswer2());
            request.setAttribute("answer3", question.getAnswer3());
            request.setAttribute("answer4", question.getAnswer4());
            request.setAttribute("QuestionID", question.getId());
            request.setAttribute("test", null);
            request.setAttribute("submitAnswer", null);
            request.getRequestDispatcher("QuestionPaper.jsp").forward(request, response);
        } else if (request.getParameter("getResults") != null) {
            questionProcessor.printResutls();
        }

我的jsp页面中有一个计时器,正在完成时间倒计时。单击提交按钮后,如何仅刷新表单。目前它正在刷新整个页面,并且在提交表单时计时器也会重置。

1 个答案:

答案 0 :(得分:0)

如何使用指向包含该表单的jsp页面的HTML iframe?这样,只有当您提交表单时,您的iframe才会更改,并且外部的计时器将继续计算。

通过集成

代码的简单示例

http://www.w3schools.com/js/tryit.asp?filename=tryjs_setinterval2

http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe

<!DOCTYPE html>
<html>

<script>
var myVar = setInterval(myTimer, 1000);

function myTimer() {
    var d = new Date();
    document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>

<body>
<p id="demo"></p>
<iframe src="http://www.w3schools.com">
  <p>Your browser does not support iframes.</p>
</iframe>

</body>
</html>