JSP获取参数问题
我有一个表单,如果计时器到达,表单将自动重定向到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否理解了这个问题,但我认为您的问题就在这里:
您正在重定向到 TestServlet,但您没有发送任何参数,因为您没有提交表单。尝试这样的事情:
希望这有帮助!
I'm not sure I have understood the issue, but i think your problem is here:
You are redirecting to TestServlet but you are not sending any parameter because you are not submitting your form. Try something like this:
Hope this helps!