JSP getParameter问题

时间:2010-12-28 08:42:56

标签: jsp servlets parameters request

我有一个表单,如果计时器到达表单将自动重定向到Servlet以更新数据库。我现在的问题是,如果javascript将窗口重定向到servlet,我的request.getParameter为null。

function verify(f,whichCase){
if(whichCase == "Submit"){
    msg = "Are you sure that you want to submit this test?";
    var i = confirm(msg)
    if(i){
        parent.window.location = "sindex.jsp"
    }
   return i;
 }
}

我这样做是因为我的jsp中有一个iframe。计时器更新问题必须使用iframe。所以,当时间到或用户点击提交parent.window.location可以让我刷新父窗口

<form method="POST" action="${pageContext.request.contextPath}/TestServlet" onSubmit="return verify(this,whichPressed)">

我的表单在用户点击提交按钮的时间内,会触发验证功能让用户确认提交。所以在我的TestServlet中我得到了这个,因为使用javascript重定向request.getParameter(“回答”)这个保持返回null。

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if (request.getParameter("Submit") != null) {
   String ans = request.getParameter("answer");
   Answer a = new Answer(ans, no);
   aa.CreateAnswer(an,t.getTestCode(),username);
   RequestDispatcher rd = request.getRequestDispatcher("/sindex.jsp");
   rd.forward(request, response);
  }
}

当重定向到TestServlet的时间触发doGet方法时,下面是我的计时器

if((mins == 0) && (secs == 0)) {
window.alert("Time is up. Press OK to submit the test."); // change timeout message as required
var sUrl = "TestServlet";
parent.window.location = sUrl // redirects to specified page once timer ends and ok button is pressed
} else {
  cd = setTimeout("redo()",1000);
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解了这个问题,但我认为你的问题就在这里:

if((mins == 0) && (secs == 0)) {
window.alert("Time is up. Press OK to submit the test."); // change timeout message as required
var sUrl = "TestServlet";
parent.window.location = sUrl // redirects to specified page once timer ends and ok button is pressed
} else {
  cd = setTimeout("redo()",1000);
}

您正在重定向到TestServlet,但由于您没有提交表单,因此您没有发送任何参数。尝试这样的事情:

    if((mins == 0) && (secs == 0)) {
    window.alert("Time is up. Press OK to submit the test."); // change timeout message as required
    var sUrl = "TestServlet";
    //myform it's a variable pointing to your form
    myform.submit();
    } else {
      cd = setTimeout("redo()",1000);
    }

希望这有帮助!