This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Refreshing the page adds a new entry in database. How to prevent this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Refreshing the page adds a new entry in database. How to prevent this?" Watch "Refreshing the page adds a new entry in database. How to prevent this?" New topic
Author

Refreshing the page adds a new entry in database. How to prevent this?

Sam Veasley
Greenhorn

Joined: Feb 15, 2013
Posts: 5
Hello,

I'm developing an application using Struts2 & Hibernate.
In one of the scenarios in this application, user can Post some text on another user's profile.
Those posts can be seen on same page with the Post details.

Following is the code used in a JSP :


When user clicks on Post button (Submit), post is getting saved properly.
But, if user refreshes this page, same Post text gets added to the DB again.

Could you please help me to prevent this addition in DB.

Thanks in advance.

Sumit Vispute.


Thanks & Regards,
Sam Veasley
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Hello Sumit Vispute ,

I am not getting you . How is it possible on page refresh ? When the page is refreshed, firstly the form should be empty , and secondly the action which will execute the saveOrUpdate() method of hibernate can only be called if the forms submit button is clicked. So by page refresh , do you mean that the submit button is clicked twice ?


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Sam Veasley
Greenhorn

Joined: Feb 15, 2013
Posts: 5
Saif Asif wrote:Hello Sumit Vispute ,

I am not getting you . How is it possible on page refresh ? When the page is refreshed, firstly the form should be empty , and secondly the action which will execute the saveOrUpdate() method of hibernate can only be called if the forms submit button is clicked. So by page refresh , do you mean that the submit button is clicked twice ?



Hello Saif Asif,

Yes, by page refresh, form "posts" might be getting submitted again.
Since, in DB, I can see the new entry of that same Post, only it's id (PK) is having newly incremented value.
I don't know the reason.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

To prevent clicking the button twice, you can use JavaScript to check if the form has been submitted and not do so twice.

For what I think you are asking about - the page refresh, there is a pattern called Post Redirect Get. Instead of just displaying a JSP after your action, you redirect to another one. This provides separation. When the user refreshes, only the get part gets refreshed and not your form submit.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ankit Dan
Ranch Hand

Joined: Aug 31, 2012
Posts: 47

it is a very general problem. and it can be dealt by redirecting the request instead of forwarding it.

See, the flow of your request would be
1. request come to servlet in any specific mode.
2. you do the specific business logic.
3. you forward request to any jsp.
right.

Now you have to make change in flow.
in third step you don't forward request, instead you redirect request. (use sendRedirect method (or an equivalent in struts framework).

Now what will happen in this case is. your work is getting done in two request. first request do the business logic. and second request displays confirmation message.

When you refresh page, then only last request will be repeated, i.e. only confirmation message, not the business logic.

But do it only for those request there is any harm due to refresh (as you told here).

Hope this helps


Ankit
Sam Veasley
Greenhorn

Joined: Feb 15, 2013
Posts: 5
Hello,

To overcome the issue, I've used type="redirect" in result tag in struts.xml, as follow :

OLD Code :


NEW Code :


After this change, earlier main issue got resolved, but new issue got introduced.
Now, after redirecting to the page, images on any page are not getting displayed.

Please help me on this.

Following is the struts.xml :


Thanks in advance.
Sam Veasley
Greenhorn

Joined: Feb 15, 2013
Posts: 5
Hello All,

Both the issues got resolved...
I've used type="redirectAction" in result tag instead of type="redirect".

Thank you very much for you kind responses.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refreshing the page adds a new entry in database. How to prevent this?
 
Similar Threads
unable to do validation for select in struts2
Struts 2.1 xhtml forms rendering invalid html
How to use struts2 submit tag as button without submitting the form ?
How to place textfield and submit button in one row by using struts2 tags?
Writing data to database