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

Conceptual Doubt

 
Vivek Hingorani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP/Servlet which has some fields on UI and a submit button and on press of submit it persists to the database.
What will happen if I press submit twice and on UI there is a primary key which is getting inserted in DB. So what will happen in response? Will there be two response one a success and one error page.
If yes then which one will be the first one?
A detailed response is highly appreciated.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen will depend of course on what you program.

Each submit will cause a separate request so you must make sure that only request is processed.

One very handy approach would be to use JavaScript on your HTML form to disable the button when the first request is sent.

Bill\
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Hingorani wrote:So what will happen in response? Will there be two response one a success and one error page.
If yes then which one will be the first one?


As @bill says, what will happen is whatever you program to happen.
The technical term for what you must do is idempotent or idempotency.
 
Vivek Hingorani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pat and William:
He didnt asked me how will I handle the same. He asked if its a simple JSP servlet application with simple JDBC persistance then what would happen in such a scenario?
I think only one response would come as HTTP is a stateless protocol but how will server react is my question.
Please respond as I am looking for the appropriate solution
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your application defines the action. It is not predefined for you by some magical servlet spec.

If your application is doing something like: find user with the name "Vivek Hingorani" and display his record,
then clicking multiple times will simply display Vivek's record.

If your application is doing something such as "delete all records with the name 'Vivek Hingorani'"
then the proper response from your application is "Vivek has been deleted"

The problem happens when your application is trying to do something such as "deposit $100 (US) into 'Vivek Hingorani's account and display the balance"
then if your starting balance is zero, the first time you click the button, it will display
Vivek Hingorani's bank account now contains $100 dollars US.
The second time, it will display
Vivek Hingorani's bank account now contains $200 dollars US.
and the third time will display
Vivek Hingorani's bank account now contains $300 dollars US.


Usually, this is not what you want the application to do.
But its not an error as far as the servlet is concerned.
 
Vivek Hingorani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Pat.
What if it creates a new record and a new id is created for my record. As a user of the application will i get one error and one success.
Will there be a flickering for the user that he sees a error and then a success response.? Need to understand conceptually
 
Tim Holloway
Bartender
Posts: 18419
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically if you submit a URL from a web browser while another URL request has been sent out, both will be processed but the rendering of the first response will be interrupted, discarded, and replaced by the second response. However this is ultimately up to the client.

On the server side, if you double-submit a request, just because one request came in before another one is no guarantee that that request will be completed before the other request is, since each request is processed in a separate thread.

There are 3 things that really annoy me in lazy online stores, incidentally.

One is failure to display the credit card expiration in the same way that the card does. That is, by month name, when the card has month number.

One is failure to allow people to enter credit card and phone numbers in readable formats with spaces, dashes, and so forth. A simple regex can normalize such things for the system's internal uses.

And the biggest one of all is when they say "don't click submit twice or you may be billed more than once", since that's a sure indicator that the developers were too cheap or too lazy to attempt what you are trying to do and ensure that the payment is actually posted only once.

Truthfully, I'm not too keen on having customer surveys and special offers jammed into my financial transactions either, but that's a different matter.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Hingorani wrote:What if it creates a new record and a new id is created for my record. As a user of the application will i get one error and one success. Will there be a flickering for the user that he sees a error and then a success response.? Need to understand conceptually


Your use of pronouns in this thread is confusing me a bit.

You seem to be missing a huge part of this concept. What your user will see depends completely on what the developer does. If they do a proper idempotent implementation, there will be no error message, you will just see the success. If they are too lazy or lack the skills to do a proper idempotent implementation, its impossible to know what it will do.

The developer of the servlet has to write the code to handle this the way they want.

There are millions of examples all over the Web that prove that many developers are too lazy to do it properly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!