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

difference between service method and other methods

Shilpa Jain

Joined: Aug 17, 2000
Posts: 16
can anyone pls. explain me why do we use service() method and what is the advantage of using doPost(), doGet() methods?
P.S actually a similar topic was posted earlier but there seems to be some confusion in the explanation
sandhya Jahagirdar

Joined: Oct 19, 2000
Posts: 2
when you make a request to the servlet then the service method will be invoked automatically and in that method depending on the header information (get,put,post,etc) that doXXX() method will be invoked. In the books they say that donot try to override the service() method for httpservlet because it handles setup and dispatching to all doXXX methods.for generic servlets you can override service() method
S Chandra Mohan
Ranch Hand

Joined: Oct 19, 2000
Posts: 75
whenever u call a servlet
the priority is the service method and then doget and
then dopost.
it is better to write codings in service than the other two

Have a wonderful day and wish u success<p>S Chandra Mohan<br />
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
It makes sense to override service() yourself only if one of the following conditions is met:
1. You are forwarding the request to another class (which may be another servlet or a JSP, for example) which itself needs to decide which method was used.
2. You are really, really sure that you will never need to respond at all to any of the other HTTP requests. Remember that HTTP is not just GET and POST; the service() method as supplied also handles things like HEAD and OPTIONS, and your server may also implement even more request types such as WebDAV.
3. If your code doesn't recognize the request type it calls super.service() to let the server code handle it.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
I agree. Here's the link:
subject: difference between service method and other methods
It's not a secret anymore!