I am preparing for the SCWCD exam using the Head First Servlets book.
I need clarification why PUT is mentioned as idempotent.
The book mentions that GET is not supposed to change anything on the server and hence considered idempotent. POST is non-idempotent because the data submitted thr' a POST is destined for some transaction that cant be reversed.
Now, PUT is used to put or store some data to the server, like a file to the server,doesnt it means it is changing something(the resources on the server,atleast) at the server, and hence , can be classified as non-idempotent?
Imagine you send TWO EQUAL requests to the server. When you use POST, and the destination servlet adds (for example) a record to a database, based on the POST information, then you will add TWO records. But if you use PUT then you will define on the server only ONE new resource, with the same content. The first request will create this resource, the second - replace just created resource with the same data, as the first resource (as the requests were EQUAL)
Can you refer to HFSJ book again?.If im not wrong i think post is a non-idempotent.I remember this only faintly not sure.Anyway i'll also refer & get back.
Idempotent : Multiple requests cause no side effects.
Non idempotent : Multiple requests may cause side effects.Business logic should take care of it.
If Post is considered as non-idempotent & post method is used then submitting the data which is present at the body would be updated in the server.If it is done more than once(by hitting the url more than once) this may cause side effect so it should be handled carefully.
Hope it is clear!!.
Joined: Jul 13, 2004
Sorry for my previous mail.I thought u r asking for a post method.Please ignore it.Neways i'll search for this question & get back shortly.
PUT is idempotent because the HTTP 1.1 spec defines it as idempotent. It is very logical. Practically we can define idempotent POST (doPost) method and non-idempotent GET (doGet) methods in Servlet, but it may treated as bad parctice.
IDEMPOTENT essentially means the ability of doing any operation again and again (i.e. more than once) without any side effects. This means every time you execute an idempotent operation, the outcome should be identical. It has nothing to do with what is changing on the server!
Since a particular PUT request will ALWAYS behave in the same manner, its idempotent!! The same is not true for a POST request.
I hope things are clear now..
(The man on the Silver Mountain)
Joined: May 28, 2005
Your'e right, Rahul..
But I discovered a clash between HTTP1.1 spec and JSP2.4 spec...
HTTP1.1 spec says 'PUT,GET,HEAD are idempotent' , but JSP2.4 spec says: " PUT NEED NOT BE IDEMPOTENT..." (;- I checked it ... )
However, dictionary meaning of idempotent : "used only once, even if used multiple times , without causing side effects.........." .. Going by that ,as u said ,PUT is idempotent ,as trying to put the same file twice will have no change on server state.....
Thnx guys for ur answers... Ravi.
Joined: May 28, 2005
Oh, Guys, Now I feel I got it wrong about JSP2.4 spec.. It says PUT Need not be idempotent ... I now understand what they meant is " the doPut() method in the servlet can be written in such a way that it can cause something else to happen" , not that the spec says "PUT IS IDEMPOTENT" .. Yeah, Sorry "SUN Micro...." :roll: