nnk
nnk

Reputation: 71

How to disable the back button in servlets?

I have created a web application and deployed it in tomcat7. This app works in the following way: User logs in through proper authentication and then a test page(html) is displayed where user selects answers and submits the test. After submitting another servlet is called where score is displayed to the user.

The problem here is once after getting the score if the user goes back(through browser back button) and submits the test again,the score is being altered.What shall i do to prevent this?

Upvotes: 0

Views: 618

Answers (1)

JB Nizet
JB Nizet

Reputation: 691765

The usual trick (which some framework provide for you) is, when the form is generated, to generate a token, store it in the user's session, as well as in a hidden field in the form. When the form is submitted, you compare the token you receive with the token in the session. If they match, you delete the token from the session and proceed with the form submission.

If the user goes back and resubmits the form, there won't be any token anymore in the session, and you'll display an error message instead of handling the form submission.

Another method is to code the form handling in order for the submission to be idempotent. For example, instead of blindly increasing the score, you can check if the question has already been answered by the user and ignore the second answer.

Upvotes: 1

Related Questions