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


Asim Abbasi

Joined: Feb 24, 2004
Posts: 2
what is the difference between doGet() doPost() and service() methods in servlet
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

From the API:
Dispatches client requests to the protected service method.
Called by the server (via the service method) to allow a servlet to handle a GET request.
Called by the server (via the service method) to allow a servlet to handle a POST request.
There is more information available in the API, but essentially the HTTP protocol supports multiple ways of making requests to a server. When the servlet container receives the request, it finds out which Servlet to send it to. The servlet service() method gets called, and it looks at the request, decides what sort of request it is and calls a doXXX() method. There are more than just doGet and doPost. You can override the service() method to manage all requests at a central point, but usually you want to retain the doXXX() methods, so if you do override the service() method, you would add some specific functionality and then still call the corresponding doXXX() method.
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 364
One additional note:
doGet is default so when calling the servlet from a hyperlink or when no action is specified in the form, this is what is called. So, I've always felt that it's better to use both and have doGet call doPost for these cases.

No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
Harro deGouw

Joined: Feb 23, 2004
Posts: 5
And since 'GET' is the default protocol used when requesting a page/servlet, it can be used to determine the state of your app.
Of course you could use a fullblown MVC framework like Struts, but consider this:
doGet: generate a form, action is same servlet with method=post
doPost: process input from the form
This way it's easy to separate the two views.
I agree. Here's the link:
subject: servlet
It's not a secret anymore!