I am new to Java Rauch.I cleared SCJP 1.6 last year and now i am preparing for SCWCD 1.5. I am refering headfast book. I had a doubt on idempotency. Theoritical concept is clear that for GET method is idempotent
same operation can be repeated number of times yielding no side effects or changes.For POST method is not idempotent,it has side effects...or some changes occuring on server side and the client is made aware of that.But i want to see a pratical example of that to see the difference in GET and POST behaviour for any update operation.Please guys ,help me by showing code to test this.I want to try in my system.I tried with one update operation i.e inserting one column in a table in db using doGet and doPost but failed in finding any difference.Please suggest and clarify
The crucial thing to understand about idempotency is that it's the developer's job to make sure that GET is idempotent. It is perfectly possible to write a doGet method that is NOT idempotent - but that would violate the HTTP specification, so you should not do that.
Also, it is perfectly possible to write a doPost method that is idempotent, and that is OK. POST does not have to be *not* idempotent - but it can be, so that's what should be assumed.
In the context of DB operations, GET would generally be used for retrieving something, maybe displaying information about a user. You can do that over and over, and it doesn't change any data - so it's idempotent. You would use POST to insert data in the DB - if you do that over and over, each time a new record would be inserted - that is not idempotent.
So, idempotency doesn't happen because code is in the doGet or doPost methods - it occurs because the developer writes it that way. It is the developer's responsibility, not the servlet container's responsibility.
Also note, idempotent doesn't mean "no side effects". It means making the same request multiple times has the same effect as doing it once. So the first time can have side effects. GET should actually satisfy a stronger condition that idempotency: it should be nullipotent, which does mean no side effects.
Deleting a resource is an example of an idempotent action -- when you do it once the result is that the resource isn't there, and when you do it again the result is the same. However you'll notice that isn't a nullipotent action, because side effects (in this case the resource disappearing) only happen on the first execution of the action.
subject: Code example explaining the meaning of idempotency