File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Code example explaining the meaning of idempotency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Code example explaining the meaning of idempotency" Watch "Code example explaining the meaning of idempotency" New topic

Code example explaining the meaning of idempotency

Rasmita Sahu

Joined: Mar 21, 2014
Posts: 1
Hi all,
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
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Welcome to JavaRanch.

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.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4542

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.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

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.
It is sorta covered in the JavaRanch Style Guide.
subject: Code example explaining the meaning of idempotency
It's not a secret anymore!