• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to stop data insertion on page refresh

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 349
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Redirect the page, after your insertion to database. Respose.sendRedirect("./yourservlet or jsp")

Hope this helps.

Ananth Chellathurai
 
Mayur Somani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mayur Somani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
response.sendRedirect approach leads to performance problems. Any other ideas?
 
Rajkamal Pillai
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic