Google News
logo
Java Servlets - Interview Questions
Explain the working of service() method of a servlet.
The service() method is actually the main method that is expected to perform the actual task. The servlet container calls the service() method to handle requests coming from the client/browsers and to provide the response back to the client.
 
Each time the server receives a request for a servlet, the server creates a new thread and calls for the service. The service() method checks the HTTP request type and calls the respective methods as required.
 
syntax :
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException{
}

The container calls the service() method and service method invokes doGet(), doPost(), doPut(), doDelete(), methods as needed. So you have nothing to do with service() method but you override either doGet() or doPost() depending on the request you receive from the client-end.

Advertisement