This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Can Anyone Explain Idempotent request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can Anyone Explain Idempotent request" Watch "Can Anyone Explain Idempotent request" New topic
Author

Can Anyone Explain Idempotent request

Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Hello,

I'm having a problem with , understanding of idempotent , AS i guess GET won't have side-effect's ,and POST have side-effect .


But it doesn't make much sense to me...... because both have side-effects. GET or POST.

Can Someone Help me OUT. (Please Explain that Idempotent Request)

Thank You
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

What sort of side effect are you talking about for GET requests?
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1071
    
    1


this link may help you understand -
http://www.coderanch.com/t/364705/Servlets/java/Idempotent-Requests
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

AS POST can access database and per /request ,and make update in database . same thing can be done using GET , accessing database per request .(so where did Idempotent Go ....Go)

Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

I guess this more like design pattern .
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41046
    
  43
It's more than a pattern. If an idempotent method has side effects, then you are violating the HTTP specification. Bad things can easily follow from that.


Ping & DNS - my free Android networking tools app
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

And why is do-Get() method is non-idempotent ,if HTTP 1.1 GET method is idempotent ,it's not making any sense.???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41046
    
  43
It's the job of the developer to ensure that his/her code does not violate specifications. It's not like there's an HTTP police that jumps into action if there's a non-idempotent doGet method being written. But it's the developer's fault nonetheless for not knowing his stuff.
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Ulf Dittmer wrote:It's the job of the developer to ensure that his/her code does not violate specifications. It's not like there's an HTTP police that jumps into action if there's a non-idempotent doGet method being written. But it's the developer's fault nonetheless for not knowing his stuff.


In Head First Servlet and JSp book they have given do-Get() method is non-idempotent and HTTP 1.1 GET method is idempotent. , how IS it ?? , than your saying what they have given is wrong ??
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

It's not that GET is automatically idempotent. It's that it should be. If you write an application with non-idempotent GET requests you are, as Ulf says, violating the specification. Anyone can violate a specification, it's just not a good idea.
Madangopal Venkatesan
Greenhorn

Joined: Jun 19, 2011
Posts: 16
Ok - Here it is!

POST does modifications on the server when the request is sent! GET does not.

POST requests are sent as HTTP payloads. GET requests are appended to the URL.

Hence, POST posts side effects while GET does not!

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Madangopal Venkatesan wrote:Hence, POST posts side effects while GET does not!

Not correct. I can write code in the doGet() method of a servlet that deletes the entire database.

GET's should not have side effects, but just because something is a GET doesn't mean that it will not. As already pointed out multiple times, it's the responsibility of the programmer to follow the rules.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
For further study, see Fielding's dissertation
Architectural Styles and the Design of Network-based Software Architectures


For the important concepts which define "RESTful" web services/applications.

Fielding is the author at the top of the HTTP 1.1 RFC.

Bill

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

The concept is easy, but a lot of the 'net does not implement it.

An idempotent action is one that if repeated, gets the same result, and results in the same state. There is no requirement that idempotent actions be GET, but they usually are.

In Bear's twisted example, a GET that deletes the whole database can in idempotent. The GET deletes the database, and returns a "database is gone" response/page/status. If you repeat it, it will still delete the database and return the same thing.

A more useful example happens with a "buy" button on a webstore. You want to press the "buy" button once, and get the item. Pressing it twice should *not* get you two of them. An idempotent action would have the "buy" function return "one item bought" and the state of the server has one item in the "bought/to be shipped" state. If you repeat it, it still should have only one item in the bought state.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Yes, Pat's example is better and points out that "side effect" is a poor way of describing the concept. "Repeatability" is better.
Suhas Mandrawadkar
Ranch Hand

Joined: Jul 21, 2007
Posts: 72

As far I remeber in Head First, while on this discussion, they have mentioned that they were talking purely in terms of "side-effects" and not "Repeatability", which holds holds true when you talk about Idempotent in any other scenario.

In HF, what they tried explaining was while the specs say HTTP GET must be idempotent, in terms of side effects, it is whole and sole responsibility of programmer to make sure that doGet is idempotent as well. There is nothing that can stop you doing anything you want from doGet.


Regards, Suhas S. Mandrawadkar.
Certifications: SCJP 6, SCWCD 5, Oracle WebLogic Server Administrator, OCE Java EE 6 EJB Developer
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

Idempotency is about repeating the same action twice and getting different results. A GET request is usually used for a read, but an update or a delete would also be idempotent... because whether you read, update or delete, the result will still be the same whether you submitted the same request once or 15000 times. If you create a record in your database, you should use a POST request, because if you submit that request twice, it will add two records, which means the state of your application will not be the same anymore. Notice I said should, not have to, because you still CAN make a GET-request non-idempotent and a POST request idempotent if you so desire... It's just bad practice.

The HTTP specification specifies that POST should be the only method that is non-idempotent. So that means whenever you write a non-idempotent servlet... You should implement doPost().


Hope that explains it.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
This discussion is getting weird.

My understanding is that a GET should return the state of a resource and produce NO side effects. So - no delete of a database or any other modification of a resource on a GET.

POST, PUT and DELETE are provided to modify resources.

This wikipedia article is rather clear.

Bill
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

^ William, you're absolutely right, but that has nothing to do with idempotency.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Anyone Explain Idempotent request
 
Similar Threads
doGet(). , Java Servlet Spec Q?
Idempotence
Idempotent request
idempotenet or NOT
IDEMPOTENT or not ?