It's not a secret anymore!*
The moose likes Servlets and the fly likes Differences Between doGet(),doPost(),service() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Differences Between doGet(),doPost(),service()" Watch "Differences Between doGet(),doPost(),service()" New topic
Author

Differences Between doGet(),doPost(),service()

ganapathy gopinathan
Ranch Hand

Joined: Aug 10, 2004
Posts: 30
Hi all,

Anyone plz help me to understand the differences between the doGet(),doPost(),and service() methods.

Thank u
gopi
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
init()
service() {goGet() & doPost())
destroy()
are the lifecycle methods. init() & destroy() are called once in lifetime. And service() method is called whenever a request comes. If request is through the Get method, service() method calls (pass that request to) doGet() and if request is through post method service() method calls (pass that request to the) doPost() method.


thanks & regards,
sanjeev.


thanks & regards,<br />Sanjeev.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Technically you can make doGet and doPost act identically. It's not uncommon to see both of those methods call doProcess or some other method you make up, allowing users to get or post at will.

The inventors of HTTP saw them as very different however. GET operations must be "idempotent" which just means they have no side effects and repeated calls should give the same result. A pure query in other words. POST operations should be used to send information to the server that will change the state of some object or data store.

HERE is an article on the topic that I happened to be reading right before I saw your note. The inventors of HTTP promote a web application architecture called REST (Representational State Transfer) that assigns very particular meaning to all the HTTP verbs - GET, POST, PUT, HEAD, etc - and further tells us that these are all we need.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Originally posted by Stan James:
GET operations must be "idempotent" which just means they have no side effects and repeated calls should give the same result.


I just wanted to make one tiny clarification--I know what you meant Stan, but I just wanted to make sure it's clear to everyone: HTTP GET operations do *not* need to always "give the same result", in terms of the *response*. The *response* can be different with each GET, as long as there are no side-effects. So what you said is correct (no side effects, pure query, etc.); I'm just making sure that everyone knows that "repeated calls should give the same result" does not mean "same RESPONSE".

cheers and thanks for the good link!
-Kathy
ganapathy gopinathan
Ranch Hand

Joined: Aug 10, 2004
Posts: 30
what is the difference between 'service()' and 'doGet()'(or 'doPost()')?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Now you haven't got the difference right. ok then read the docs or some tutorial, book.
Again there is no better substitute.

cheers
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75

what is the difference between 'service()' and 'doGet()'(or 'doPost()')?

service(), doGet() and doPost() are methods, and are used to do operations (methods are to do operations/task, hope that's clear).
you can perform the same or different task in doGet() & in doPost() as per your need(requirement). So, there is no difference b/w doGet() & doPost() and you need to implement these methods as per your requirements. At the moment you should know when doGet() would be called & when doPost() would be called and how.



Now you haven't got the difference right. ok then read the docs or some tutorial, book.
Again there is no better substitute.

do the same & share your doubts.



thanks & regards.
ganapathy gopinathan
Ranch Hand

Joined: Aug 10, 2004
Posts: 30
Correct me if i m wrong,

GET():

- 'Get' is used to send the form data with the request itself.
- only 256 characters are allowed.
- when there is no such class or nothing to satisfy the request the data is lost.

POST():
- first header is send, the form fields are send after the finding the something that satisfies the request.
- i think there is no limitation on no of charactors to be sent.
- if there is nothing to satisfy the request then the data is not sent.

let me know in which conditions or situation we have to use the service()?

Thanks
gopinathan
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
hey gopi i found something simple and somewhat complete for you. here is that,

After the servlet is initialized, the container may keep it ready for handling client
requests. When client requests arrive, they are delegated to the servlet through the
service()method, passing the request and response objects as parameters. In the
case of HTTP requests, the request and response objects are implementations of
HttpServletRequest and HttpServletResponse respectively. In the
HttpServlet class, the service() method invokes a different handler method for
each type of HTTP request, doGet() method for GET requests, doPost() method for
POST requests, and so on.


some more,


GET method

The GET method is used to retrieve a resource (like an image or an HTML page) from
the server, which is specified in the request URL. When the user types the request
URL into the browser's location field or clicks on a hyperlink, the GET method is
triggered. If a tag is used, the method attribute can be specified as "GET" to cause the
browser to send a GET request. Even if no method attribute is specified, the browser
uses the GET method by default.
We can pass request parameters by having a query string appended to the request
URL, which is a set of name-value pairs separated by an "&" character. Here we have passed the parameters studname and studno, which have the values
"Tom" and "123" respectively. Because the data passed using the GET method is
visible inside the URL, it is not advisable to send sensitive information in this manner.
The other restrictions for the GET method are that it can pass only text data and not more than 255 characters (i read somewhere that it depends on browser support).


POST method
The purpose of the POST method is to "post" or send information to the server. It is
possible to send an unlimited amount of data as part of a POST request, and the type of
data can be binary or text.
This method is usually used for sending bulk data, such as uploading files or updating
databases. The method attribute of the <form> tag can be specified as "POST" to
cause the browser to send a POST request to the server.
Because the request parameters are sent as part of the request body, it is not visible
as part of the request URL, which was also the case with the GET method.
[ October 01, 2004: Message edited by: adeel ansari ]
srinivasan doraiswamy
Greenhorn

Joined: Sep 30, 2004
Posts: 5
when the req is received by the service method, req and res are cast to their http counter parts and service method with http req and res is invoked, this method directs the request to the appropriate method based on get or post, actually u no need to implement the service method instead u should implement these subsidiary methods (get or post) in ur servlet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Differences Between doGet(),doPost(),service()