• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sam Veasley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Sam Veasley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Dan
Ranch Hand
Posts: 47
Eclipse IDE Java MyEclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic