File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to stop data insertion on page refresh

 
Mayur Somani
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ananth Chellathurai
Ranch Hand
Posts: 349
Eclipse IDE Hibernate Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
K. Tsang
Bartender
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
response.sendRedirect approach leads to performance problems. Any other ideas?
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
ganeshprabu somaskandan
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Tamas Jano
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic