aspose file tools*
The moose likes Servlets and the fly likes Multiple rows inserted coz of page refresh Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple rows inserted coz of page refresh" Watch "Multiple rows inserted coz of page refresh" New topic
Author

Multiple rows inserted coz of page refresh

Lakshmi Dasari
Ranch Hand

Joined: Mar 03, 2003
Posts: 35
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,


Lakshmi
Chris Smith
Ranch Hand

Joined: May 03, 2002
Posts: 42
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.
Alok Pota
Ranch Hand

Joined: Mar 07, 2001
Posts: 185
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.
Lakshmi Dasari
Ranch Hand

Joined: Mar 03, 2003
Posts: 35
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,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multiple rows inserted coz of page refresh