Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of the servlet service method

I thought, that we cannot override service() method in any specific servlet. So what is purpose of httpservlet service method?

like image 399
St.Antario Avatar asked Jul 18 '14 09:07

St.Antario


2 Answers

From **service method ()** only your actual method (get,post ...etc) decides to call.

The default service() method in an HTTP servlet routes the request to another method based on the HTTP transfer method (POST, and GET). For example, HTTP POST requests are routed to the doPost() method, HTTP GET requests are routed to the doGet() method. This routing enables the servlet to perform different request data processing depending on the transfer method. Because the routing takes place in service(), you do not need to override service() in an HTTP servlet. Instead, override doGet(), anddoPost() depending on the expected request type.

like image 62
Suresh Atta Avatar answered Sep 20 '22 20:09

Suresh Atta


The servlet service() method that perform the task of determining the method that has been called i.e. get/post/trace/head/options/put/delete. These are the 'big seven' methods since they are the most commonly used ones.

After determining the method which is actually called,it then delegates the task to the correspondin method.

You can either use,

public void doGet(javax.servlet.http.HttpServletRequest request,  
                  javax.servlet.http.HttpServletResponseresponse)
            throws javax.servlet.ServletException,java.io.IOException {...}

or,

public void doPost(javax.servlet.http.HttpServletRequest request,  
                  javax.servlet.http.HttpServletResponseresponse)
            throws javax.servlet.ServletException,java.io.IOException {...}

instead of,

public void service(javax.servlet.ServletRequest request,  
                  javax.servlet.ServletResponseresponse)
            throws javax.servlet.ServletException,java.io.IOException {...}
like image 40
user3589907 Avatar answered Sep 16 '22 20:09

user3589907