Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Preventing Form Resubmission

 
clay jay
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies in advance as I've searched through the forum for solutions and while I've come across useful information I still haven't managed to get(or understand at least) a proper implementation of this for my somewhat different situation.

Here is the flow of my web service:

JSP page 1 contains drop down boxes. user selects different values then submits form.
Servlet does some database work depending on what was selected and certain conditions being met forwards to JSP page 2
JSP2 has a form generated depending on values of drop downs in JSP1, user fills out form and submits
Servlet verifies data and sends it off to the database and redirects user to a success page

I would not like the user to simply hit back and then resubmit the form so I've added.



Now they are prompted if they'd like to resend the data and if they click yes the form is simply regenerated depending on the drop down boxes in JSP1 and they are again able to resubmit the form if they'd like.

The thing is, user's are allowed to submit as many forms as they like depending on conditions being met in the drop downs of JSP1 so its not like I want to prevent the user from ever submitting the form again I just don't want them to be able to hit back and resubmit the form for the exact same values. I want it to be required that they come from JSP1 first.

Thanks in advance for any help.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add a session variable into a form[hidden variable]. and remove[or change] the session value after the first submit.

also search for "PRG Technique" in javaranch
 
clay jay
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply seetharaman

I did unsuccessfully attempt the session variable technique but not the exact way you describe above. Unless I'm misunderstanding you but in your method if a user hits back and is returned to the form will it not re set that session variable right there thus allowing them to resubmit the form again?

As far as the PRG technique, I have applied it at least in the sense that after JSP2 form is submitted to the servlet and database work is taken care of they are redirected to a success page which prevents any double submit problems on refresh but leaves me with the problem I described above.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this out
 
clay jay
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks for the help guys, After reading more about the PRG pattern especially from Bears article here I've realized that my current system has the page controller and task controller combined which is the reason this problem occurs. If I set the session attribute in my Servlet1 and then forward to JSP2 then my request is passed all the way from JSP1 - Servlet1 - JSP2 - Servlet2 then finally the request is lost hwen the user is redirected to the success page. When the user hits back they are asked to resend the request because I disabled caching and the request comes all the way from JSP1 as it was forwarded all the way down and thus the session attribute I set which was removed in Servlet2 to prevent the user from resubmitting the form again gets re set when I don't want it to.

The reason why I forwarded the request all the way down is because on JSP1 there are two parameters I need from text boxes that I also need in JSP2 so If I don't forward the request I lose those parameters unless I store those in the session or something like that. I'm guessing where I'm going wrong is my JSP2 shouldn't be dependent on those parameters and the task controller should use them the way I need and store them in a bean or something before I even come to JSP2. Ah this is so frustrating :/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!