JSP getParameter problem
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);
}
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!
精彩评论