How a request is mapped to doGet() or doPost()?

Have you ever thought how a request is mapped to doGet() and doPost() methods that we define in our Servlet classes. The Servlet interface which defines the life cycle of servlets provides service() method for request processing.

The server doesn’t know doGet() and doPost() methods. It simply invokes service() method whenever a request is received for a servlet. The mapping of service() to doGet() and doPost() is done by HttpServlet class. You may recall that GenericServlet class doesn’t define service() method and is extended by HttpServlet class. We extend HttpServlet but don’t define service() method and doesn’t declare out classes as abstract which implies that the service() method is defined by HttpServlet class.

Lets find out how a request is mapped to doGet() and doPost() methods.

Actually the HttpSevlet class has two service() methods, one protocol independent that is known to the server and one protocol specific which is not known to the server. These methods have following signature:

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException;

protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException;

The HTTP specific method is protected i.e. it is not visible to the server. It is defined to be internally invoked. This method is responsible for mapping a request to HTTP specific methods on the basis of request type. It is invoked from the generic service() method which is invoked by the server.

Following diagram describes how a request is mapped to doGet() or doPost() method.

request mapping to doget

Description of the diagram:

1.0 – First request lets say get, is submitted for a servlet by the client.

1.1 – Servlet object is created by the server.

1.2 – ServletConfig object is created by the server for the servlet.

1.3 – init() method is invoked by the server and reference of the ServletConfig object is provided as argument.

1.4 – init() method of GenericServlet is executed because neither HttpServlet not UserDefinedServlet override it.

1.5 – service() method is invoked by the server and references of ServletRequest & ServletResponse objects are provided as arguments.

1.6 – Generic service() method of HttpServlet is executed. This method type casts ServletRequest & ServletResponse to HttpServletRequest & HttpServletResponse and invokes HTTP specific service() method.

1.7 – The HTTP specific service() method checks the request type and invokes the method accordingly. In this case, we have assumed the request type to be get hence doGet() is invoked by the protected service() method of HttpServlet.

1.8 – HTTP specific metod of the UserDefinedServlet, doGet() in this case, is executed.


Its time to recap the main concepts:

  1. When a request is received for a servlet, server invokes the service() method.
  2. Generic service() method of HttpServlet class gets executed.
  3. Generic service() method invokes the HTTP specific service() method of HttpServlet class.
  4. HTTP specific service() method checks request type and invokes doGet(), doPost() methods.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *