JSP获取参数问题

发布于 2024-10-09 09:40:09 字数 1572 浏览 0 评论 0原文

我有一个表单,如果计时器到达,表单将自动重定向到 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("answer") 这一直返回 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);
}

I have a form, if the timer reach the form will auto redirect to the Servlet to update database. My problem now is if javascript redirect the window to servlet my request.getParameter is 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;
 }
}

I doing this because i got a iframe in my jsp. Timer update problem have to use iframe. So, when time up or user click submit parent.window.location can let me refresh parent window

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

My form when user click submit button within the timing, it will trigger the verify function to let user confirm submit. So inside my TestServlet i got this, because using javascript redirect request.getParameter("answer") this keep return me 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);
  }
}

Below are my timer when time up redirect to TestServlet trigger the doGet method

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2024-10-16 09:40:09

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

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

希望这有帮助!

I'm not sure I have understood the issue, but i think your problem is here:

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

You are redirecting to TestServlet but you are not sending any parameter because you are not submitting your form. Try something like this:

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

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文