aspose file tools*
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
Author

servlet

Asim Abbasi
Greenhorn

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

Joined: Mar 06, 2001
Posts: 13459

From the API:
javax.servlet.http.HttpServlet.service()
Dispatches client requests to the protected service method.
javax.servlet.http.HttpServlet.doGet()
Called by the server (via the service method) to allow a servlet to handle a GET request.
javax.servlet.http.HttpServlet.doPost()
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.
Dave
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
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
Greenhorn

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: servlet
 
Similar Threads
javascript, servlets and SVG
How can I change the sdk version of Websphere 4 if I already install it
Capturing Form Data
Export in Excel
Is there any way to get Servlet Init Parameter using EL?