I am writing a program, but i encounter a problem: when I refresh the jsp page, system will automatically resubmit the whole page, and i don't know how to avoid it, can someone help me ?
One way to stop page resubmission on page refresh is to unset the form data after it is submitted so that the variable storing form data becomes empty and wrap up your form processing block of codes to check if the form is empty.
You can prevent form resubmission via a session variable. Yes we can use microtime() as well as time() also instead of rand() , whatever function or variable that gives different value we can use it. BUT make sure that you set that value to SESSION variable.
Solution 1: Disable Confirm Form Resubmission From Chrome Right click on your chorme shortcut, select properties. In the target field, add: “-disable-prompt-on-repost” without the quotes after chrome.exe.
First thing you can do is you need to add required field validations to the textboxs. Then you have to empty the texboxes in the button_click event after sqlcommand. You can empty the texboxes using TextBox1. Text = String.
Here's an explanation of the problem...
Clicking the "submit" button on a form sends a request to the web server, which includes all the data entered on the form. Not only the URL but also the form data is part of the request, and this request is remembered by the browser. If the user clicks "refresh", the browser repeats the request, sending the same URL and form data to the web server again.
But forms can be submitted in two different ways, GET or POST, depending on the "method" attribute of the "form" tag. There is a convention that a GET request has no side-effects; it only fetches data but does not make any changes to the database. On the other hand, if a request changes data it should always use a POST request. As I said, these are only conventions, and there is not much technical difference between them, but a very important difference is that browsers will warn the user if they try to repeat a POST -- clicking "refresh" will pop up a dialog box warning the user that this may cause an operation to be repeated, and confirming that they really want to resubmit. The browser does not show this confirmation when refreshing a GET request.
Is your form using the GET method, as suspected by @mk? If so, changing it to POST is the simplest solution, since this will at least mean that the user is warned if they try to refresh.
But a better solution is the POST+REDIRECT+GET idiom suggested by @cletus. This splits the database update (POST) and the view (GET) into two operations. Clicking refresh on the browser then merely repeats the GET, which has no side-effects.
The idiom you want here is either:
POST+REDIRECT+GET works like this:
The benefit of this is that clicking reload won't resubmit the form. Also if you click the browser back button you won't get prompted with the "submit again?" dialog box.
Here is a JSP example of this.
The AJAX submit means that instead of submitting the form back to the server in the traditional sense you create an AJAX request back to the server with the form data. That submit does what it needs to. Clicking reload will simply reload the page. It won't resend the AJAX request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With