St.Antario
St.Antario

Reputation: 27385

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?

Upvotes: 5

Views: 2042

Answers (2)

user3589907
user3589907

Reputation: 243

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 {...}

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

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.

Upvotes: 5

Related Questions