aspose file tools*
The moose likes Servlets and the fly likes How to stop data insertion on page refresh Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to stop data insertion on page refresh" Watch "How to stop data insertion on page refresh" New topic
Author

How to stop data insertion on page refresh

Mayur Somani
Greenhorn

Joined: Apr 14, 2008
Posts: 26
Hi ,I am working on jsp with mysql.
Whenever I refersh the page, the value on form is inserted again into table.
I researched a bit to solve this problem and came up with the following solutions-

=>redirect to another page after insertion(doesn't work)
=>disable cache(doesn't work either)

Is there any other way to prevent this?
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

I Assume you have an Action (preferred approach) or otherwise code in JSP to insert the data into the Db (bad design). Either ways from where the request is being serviced if you check if the values are already in the database you can find out if the particular request is from a page refresh.

Cheers,
Raj.
Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

Redirect the page, after your insertion to database. Respose.sendRedirect("./yourservlet or jsp")

Hope this helps.

Ananth Chellathurai


Ananth Chellathurai [Walk on software]
Mayur Somani
Greenhorn

Joined: Apr 14, 2008
Posts: 26
@RajKamal
Yes I know that it is bad design but I was not developing an enterprise app. It was just a little experiment.

@Ananth
I already tried that approach. It didn't work.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2419
    
    7

The page redirect (response.sendRedirect) approach should work. Assume you do your db insertion in your servlet, refreshing the page of the "redirected" page should not trigger the insertion again.

However, using RequestDispatcher.forward will trigger the insertion again.

By the way for redirect, the url changes and that page is independently from your servlet or whatever page you do your insertion code.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Mayur Somani
Greenhorn

Joined: Apr 14, 2008
Posts: 26
response.sendRedirect approach leads to performance problems. Any other ideas?
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

Tried what I had mentioned earlier?


Either ways from where the request is being serviced if you check whether values are already in the database you can find out if the particular request is from a page refresh.


Cheers,
Raj.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

use a counter with session. send the counter as query string or hidden form field on every response page. now whenever you receive a request check the value of the counter. if the value received from the request is less then the counter value in the session, then it is a request from a refresh. if the counter from the request is equal to the session counter then the request is through some action. then do the action, add one to the counter in the session and send the new counter to the user.

I think this will solve your problem....
[ August 22, 2008: Message edited by: Ankit Garg ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Mayur Somani:
@RajKamal
Yes I know that it is bad design but I was not developing an enterprise app. It was just a little experiment.


And the bad design is now limiting your functionality. Time to fix it.

1) Your form posts to a Servlet (and has all the no-cache headers set).
2) The Servlet processes the form by putting data into the database
3) The Servlet uses response.sendRedirect(...) to a display page
4) The display page shows the data and when you refresh does not re-post. Pressing the back button won't repost either.

This will work.

Originally posted by Mayur Somani:
response.sendRedirect approach leads to performance problems. Any other ideas?


And you tried it and measured it? What kind of performance problems?


Steve
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

The so-called "Post-Redirect-Get pattern" that Steve describes is discussed as part of this article, and is the best and proven solution to avoid the described problem, mythical performance "problems" or not.
[ August 22, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ganeshprabu somaskandan
Greenhorn

Joined: May 19, 2003
Posts: 3
you should design such a way that the user submit the answers for all questions in a single submit button. Then impose counter session concept(double submission)

This will give user the "Review" functionality for your online application.


s.ganesh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

"yesganesh", you have previously been warned regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Be aware that accounts with invalid display names are disabled.

bear
JavaRanch Sheriff
Tamas Jano
Ranch Hand

Joined: Feb 21, 2007
Posts: 55
Try what Ankit Garg has said...a really easy thing to do is just put a hidden field containing the timestamp onto the form you are submitting and on the action (servlet) check for it on the session.
If it exists and is not the same OR is not on the session do your stuff and save it onto the session so if you resubmit with refresh the equals will evaluate to true and will skip the action.
It is called the Synchronizer Token Pattern.
Just google for it and choose an article to get some in-depth knowledge about it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to stop data insertion on page refresh