Q. How do you prevent multiple submits due to repeated “refresh button” clicks?
Problem: Very often a user is completely unaware that a browser resends information to the server when a
“refresh button” in Microsoft Internet Explorer or a “reload button” in Netscape/Mozilla is clicked. Even if a browser
warns user, a user cannot often understand the technical meaning of the warning. This action can cause form data
to be resubmitted, possibly with unexpected results such as duplicate/multiple purchases of a same item,
attempting to delete the previously deleted item from the database resulting in a SQLException being thrown.
Non-idempotent methods are methods that cause the state to change. But some operations like reading a list of
products or customer details etc are safe because they do not alter the state of the model and the database.
These methods are known as idempotent methods.
Solution-1: You can use a Post/Redirect/Get (aka PRG) pattern. This pattern involves the following steps:
Step-1: First a user filled form is submitted to the server (i.e. a Servlet) using a “POST” (also a “GET” method).
Servlet performs a business operation by updating the state in the database and the business model.
Step-2: Servlet replies with redirect response (i.e. sendRedirect() operation as opposed to the forward() operation)
for a view page.
Step-3: Browser loads a view using a “GET” where no user data is sent. This is usually a separate JSP page,
which is safe from “multiple submits”. For e.g. reading data from a database, a confirmation page etc.
JSP Interview Questions
Monday, July 20, 2009
|
digg it
Leave your comment
Posted by Sitesh Gupta
No Responded To This Post
Leave A Reply