How Service() Method will Handle Requests

The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.

Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.

Here is the signature of this method:

public void service(ServletRequest request, ServletResponse response)

throws ServletException, IOException

{

// code….

}

  • The service () method is called by the container and service method invokes doGe, doPost, doPut, doDelete, etc. methods as appropriate.
  • So you have nothing to do with service() method but you override either doGet() or doPost() depending on what type of request you receive from the client.
  • The doGet() and doPost() are most frequently used methods with in each service request. Here are the signature of these two methods.