Hi all, My application is three tiered and is based on the MVC architecture. I am facing a problem regarding insertion of multiple redundant rows in the database due to page refreshes. How do I identify if the request is the same as before or is a new request. Where and how should the check be done? Regards,
Two options make sense: 1. On each of your forms that should not be repeated, include a hidden field with a unique token identifying that form. Keep, in the session, a Map of hidden fields to responses. If you get back a request with this token field identical to a previous one, just return the previous response, rather than repeating the action itself. 2. Alternatively, instead of forwarding to your response using RequestDispatcher, use a sendRedirect... that way, when a user refreshes, they will reload the URL for the response page, which can be idempotent, rather than the URL that triggers the action itself.
Have a primary key on the table where you are inserting? Thats the first thing you need to do maintain data integrity. You will get an exception instead of duplicating rows, but atleast you won't be messing up the data.
Joined: Mar 03, 2003
Hi Chris, Thanks for your reply.
Keep, in the session, a Map of hidden fields to responses.
My application does not use session. I did think of this option, but I have to work around the problem without the use of session.
Alternatively, instead of forwarding to your response using RequestDispatcher, use a sendRedirect.
I am forwarding the request using RequestDispatcher, as I have packaged my request object with attributes, which are needed by the next servlet/jsp in the chain. I tried using sendRedirect but it does not achieve the purpose. So any other alternatives would be highly appreciated. Alok,
Have a primary key on the table where you are inserting.
I already have the primary key constraint, but the primary key field is generated whenever a new request arrives. The problem here is to identify if the request is new or the same as in the case of page refresh.. Any thoughts on this?? Thanks all for your interest and responses. Regards,
subject: Multiple rows inserted coz of page refresh