Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Help needed in preventing data being inserted twice.

Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I have a jsp from which i post data to a controller servlet now the problem for me is if i am inserting data from jsp thru a save button and if i click the button in quick succession the data gets inserted twice. i can disable the buttons on the jsp but the solution will need to be implemented in every jsp. can it in any way be handled in the controller servlet so that the implementation remains only at one place and hence easily managable.
Thanx all in advace.
Devesh H Rao.
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the solutions is to use a token. Before you display the JSP, create a random value (token), put it in the Session and place this value as a hidden value in your form.
when the user clicks the save button for the first time, your servlet will get the value from the hidden field (request.getParameter("myToken"))and compare it against your value in the Session. If the values are the same, remove your token from the session and process the data from the form (insert it to the DB).
Now , if the user hits the button again, you're application will try to compare again the value in the hidden field with the one in the session. Since you removed the value from the session, you know that the user pressed the button more than once, so you can discard that request and forward it directly to a thankyou.jsp, for example.
hope it helps..
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic