If you use HTTP Protocol, you should extend HTTPServlet class and override doGet(), doPost(),etc methods. Don't override service() method ! If you need to execute the same business logic when both POST or GET methods are invoked, just make one method call the other one where all the logic is(For example, say that you write all the business logic in doGet() method. Then in doPost method, you should invoke doGet() method). HTTPServlet class has service() method, and the default behaviour is that when any request is made by client, the service() method is invoked in the lifecycle of servlet. service() method has the responsibility of parsing the request and invoking the appropriate doX() method. If you overrride service() method in a subclass of HTTPServlet, only service() method is called for all types of requests(e.g. post request, get request, put request, etc. ). doPost, doGet, doPut methods are not called if you don't call them in service() method explicitly. You have the responsibility of parsing the request message, and doing something with that. service() method does not call doX methods unless specified explicitly. All control is in your hands! If you want to develop a servlet which does the same thing for all types of requests(i admit that this is a very weird and rare scenario), override service() method.
If you use a protocol other than HTTP, you should extend GenericServlet class and overrride service() method. If you will response to requests made by a client that is not using the HTTP protocol, you must override service() method(). There is no other choice other than that! For all types of requests, service() method is invoked. There is no doGet, doPost, doPut, etc. methods in GenericServlet class.
Quoting From Oracle's site:
"The default service() method in an HTTP servlet routes the request to another method based on the HTTP transfer method (POST, GET, and so on). For example, HTTP POST requests are routed to the doPost() method, HTTP GET requests are routed to the doGet() method, and so on. This enables the servlet to perform different request data processing depending on the transfer method. Since the routing takes place inservice(), there is no need to generally override service() in an HTTP servlet. Instead, override doGet(), doPost(), and so on, depending on the expected request type."
To sum up,
For HTTP protocol, extend HTTPServlet class, don't override service() method, override only doX methods.
For other protocols, extend GenericServlet class, override service() method. There is no doX method in GenericServlet class.
Bu blog, yazılım ağırlıklı olmak üzere çeşitli konular hakkında makaleler içermektedir. ( This blog includes various topics related to software development. )