wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doGet idempotent? 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doGet idempotent?" Watch "doGet idempotent?" New topic
Author

doGet idempotent?

Shreyas Reddy
Ranch Hand

Joined: Oct 09, 2003
Posts: 49
From java docs:
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException
Part os the method description says:
The GET method should also be idempotent, meaning that it can be safely repeated. Sometimes making a method safe also makes it idempotent. For example, repeating queries is both safe and idempotent, but buying a product online or modifying data is neither safe nor idempotent.
What happens if you do have some code in the doGet(,) method that modifies data? Does the container consider the request as a "Bad request"?'
Thx,
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

The servlet specification does not obligate the container to check if any data have been changed in the course of servicing a GET reqyuest. Thus it is the programmer's responsibility alone to ensure that a doGet() method is idempotent.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Sathya Sankar
Ranch Hand

Joined: Sep 16, 2000
Posts: 67
Hi Michael,
Need a clarification on the implication of the doGet method being idempotent. When we say that repeated GET requests for the same URL should return the same respone, does the URL include the querystring too?
Or is it that irrespective of the query string too the response should be the same?
Thanks,
GSS


SCJP, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
Shreyas Reddy
Ranch Hand

Joined: Oct 09, 2003
Posts: 49
For a particular query string, the response remains the same over multiple requests. So if a query string changes, the response changes, but will not change for the same query.
So a GET request is NOT supposed to change state on the server, but the container is(will) not preventing it.
Correct me if I am wrong.
Thx,
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Well said, Shreyas!
The idea of idempotency gets stretched a little in practice. In short, one query should produce one result. Another way of saying this is the the query itself should not cause an unexpected state or behavior change.
It's hard to come up with good examples. A very weak example is something like a hit counter. You could argue that hit counters are not idempotent, but because this behavior is expected and consistent, we don't think of idempotency as being relevant. A more radical illustration: every time sometimes GETs a certain web page on the tenth minute of the hour, say, the hosting server triggers a process to clear all current connections to the database. Clearly, that's not idempotent. It doesn't matter if the behavior is intended or accidental.
When a method is idempotent, in short, you are guaranteeing that regardless of when or how often it's used, no "side effects" will occur.
Sometimes this is a tougher standard to meet than you might think, especially when you have code running to create some or all of a web page's content.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doGet idempotent?
 
Similar Threads
Get and Post Method
doGet(). , Java Servlet Spec Q?
idempotent
about idempotent?
Using get() and post() together