• 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

F5 causes action to be executed again

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
I'm working on a Struts-based web application that's giving me a bit of grief when the page is refreshed.
For certain actions the user can perform, records are inserted into a database. If the user then refreshes the page, the same action gets performed and another record gets inserted.
How can I ensure that, after submitting an action, the page is not left in a state where the same action will be executed again by refreshing the screen?
Cheers,
Kevin
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are lots of solutions to this. One way is to generate a key that goes along with the form data. The key is added to some map when the form is created. When the user clicks once the key is removed from the map. If the user clicks again, you check if the key exists and because it doesn't you don't handle the command.
You could also use a redirect when you return the next view after you handle the form submission. (I think you can do that with Struts.) I don't think there any "out of the box" solutions to this. But hopefully these ideas will get you started on your own solution.
 
Kevin Stembridge
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nathaniel,
I followed your second suggestion and made a call to response.sendRedirect() just before returning from the Struts Action class.
Worked a treat
Thanks very much.
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Struts and Other Frameworks Forum...
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, there is an "out of the box" solution if you're using Struts. You can use the saveToken(), resetToken() and isTokenValid() methods of the Action class. Redirect may work but I have run into problems using that approach in certain situations (forgot the details but had to do with losing the context).
Basically, you'd have this kind of code in your Action:
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Junilu's method. The token is very easy to use and it will also prevent strange things from happening when the user tries using the browser's Back button and resubmitting an out-of-order page.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic