After a user submits an html form (with method=”post”) it brings them back to the page they were on (but with some of the data changed). If the user refreshes the page, the form is submitted again. In my application this can create an illegal state (since the form data is submitted without going through verification).
How can I prevent the form from being submitted when the user refreshes the page?
Here is some sample code to give you a basic idea of what is going on:
<script type="javascript">
function verifyForm() {
//do stuff
}
...
<form onsubmit="return verifyForm();" method="post">
<input name="testing" type="text" size="8" />
<input name="save" type="submit value="Submit Form" />
</form>
Then in the JSP controller:
public class myController implements Controller
{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
// do stuff with the form data received in the request
}
}
There are several places I might be able to solve this problem: in the html page itself, or in the controller (using MVC pattern in jsp). So far I have found this question that almost seems to apply to my question, but doesn’t quite seem to be asking the same thing (also I am not using PHP).
You need to use the PRG pattern: Post/Redirect/Get.
Read about it here: http://en.wikipedia.org/wiki/Post/Redirect/Get