I am looking out for a better logic to save the form values entered by user, as the user accidently traverse to nextpage by clicking submit button.when he clicks on back button , the values should exist.please let me know your thoughts.
You could do it with Javascript, just save the values to a cookie when the user exits the page and load them when the user returns.
You could also store it in the session, but this will be performance-intensive on the server side. If you have a few fields (say 2 Kb per user in all) then this can work, especially is useful if your form is split across more than 3 pages or so.
Also remember to expire the objects from the session once the form is submitted on the last page.
Have a look into articles about REST, the architectural style which the whole web is based upon. By following these guidelines about client-side state storage, you can have your web app show the user the correct information when something unexpected happens. http://www.xfront.com/REST-Web-Services.html. You essentially want the store the entire context on the client (in a cookie, or held in the current page and transferred back to the server using POST), and not take advantage of any context on the server. In doing this, the client can bootstrap the process at their current 'position' in the process using their local context, even if the server has no stored context.
If you submit a form using POST
and you use the browser back button to go back to this page, then you're dependent on the HTTP response caching settings, the browser cache settings and the browser default behaviour whether it will respawn the input values or not. Most will respawn the values by default, but you don't want to be dependent on the client behaviour. If caching is disabled, then for example IE will still respawn the values, but FF won't. Also, if caching is forcibly disabled from the server side on, then the client may face an intrusive and browser-specific "Are you sure to resend the data?" confirmation dialogue.
Your best bet is to store the submitted data in the session scope and provide a Back
button in the form yourself next to the Submit
or Next
button. This way it's more intuitive for the enduser to navigate back.
You can make use of HttpSession#setAttribute()
to store the submitted data in the session scope before forwarding the request to the next page:
HttpSession session = request.getSession();
session.setAttribute("data", data);
In the subsequent pages you can of course get it back by HttpSession#getAttribute()
, so that you can add/modify the new values of the next steps.
Data data = (Data) session.setAttribute("data");
In the pages you can use EL the usual way to redisplay the submitted data in the value
attribute of the input fields:
<input type="text" name="foo" value="${data.foo}">
Providing a Back
button yourself however does not prevent the user from clicking the browser's own back button or pressing Backspace anyway. You can't do much against this. Your best bet is to strictly disable page caching so that the client will get the "Are you sure to resend the data?" warning. You can forcibly disable the client side caching from the server side on by adding the following headers to the response of the page in question:
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.