Arvind
Arvind

Reputation: 6474

Should I override service() or doPost()?

I was reading a book on servlets, in that book a brief explanation is given about the servlet class, as well as the HttpServlet class.

There is one example for filling in a form- for that form, the servlet's doPost() method is overridden by the class. But for another example of a login form, the service() method is overridden instead.

I want to know why the 2 different approaches- I thought that usually we put our custom code into doPost() (or doGet()) and let service() remain as it is. Is there any reason behind using either one of the 2 approaches, or can I use both approaches in any situation?

Upvotes: 13

Views: 17887

Answers (5)

sky
sky

Reputation: 55

The service() method belongs to Genericservlet and can be overloaded to support any type of protocol such as Http,Ftp etc.

Then you have specialized servlet for handling HttpProtocol, we call it HttpServlet. The HttpServlet also provides default implementation for service() and doGet() and doPost() methods.

Why we should not override the service() method?

Since It's not a good practice to override the service method. If we call any of the doxxx method then internally it will call the service method of the HttpServlet. So there's no need for you to call it explicitly.

Order of Execution of service():

service(ServletRequest,ServletResponse)-->

-->calls

-->service(HttpServletRequest req,HttpServletResponse res)

-->calls

-->doGet/doPost(HttpServletRequest req,HttpServletResponse res)

This is how you can override the service in case you want to:

protected void service(HttpServletRequest req, HttpServletResponse resp) {
String method = req.getMethod();

if (method.equals(METHOD_GET)) {
        doGet(req, resp);
} else if (method.equals(METHOD_HEAD)) {
    doHead(req, resp);
} else if (method.equals(METHOD_POST)) {
    doPost(req, resp);

} else if (method.equals(METHOD_PUT)) {
    doPut(req, resp);   

} else if (method.equals(METHOD_DELETE)) {
    doDelete(req, resp);

} else if (method.equals(METHOD_OPTIONS)) {
    doOptions(req,resp);

} else if (method.equals(METHOD_TRACE)) {
    doTrace(req,resp);

} else {
    resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}}

Implementation code given by Tomasz Nurkiewicz from SO community only Overriding Service Method

Upvotes: 1

user2768783
user2768783

Reputation: 1

If you must respond to GET or POST requests made by a HTTP protocol client (usually a browser) don't hesitate to extend HttpServlet and use its convenience methods. If you must respond to requests made by a client that is not using the HTTP protocol, you must use service()

Upvotes: 0

Amir Raminfar
Amir Raminfar

Reputation: 34169

Do not override service() method. The preferred approach is using doPost() for post and doGet() for get. Here is an excellent post on what each does. http://www.jguru.com/faq/view.jsp?EID=47730

If you must respond to requests made by a client that is not using the HTTP protocol, you must use service().

Upvotes: 14

Costi Ciudatu
Costi Ciudatu

Reputation: 38235

I think you need to understand the flow in order to decide for yourself. The default implementation of service() for an HttpServlet simply calls the appropriate handler for the request method (GET, POST, whatever).

You need to override service() when you want the same method to handle all incoming methods (no matter if it's a GET, PUT or POST request, you'll answer the same to all). If you're happy with treating each method separately, go with the default service() implementation and override the specific handlers.

Upvotes: 4

asgs
asgs

Reputation: 3984

You most probably override the doXXX() method where XXX stands for the HTTP Methods like GET, POST, and so on. service() method invoked by the container will decide which of the doXXX() to be called.

Upvotes: 1

Related Questions