This question already has an answer here:
How does the servlet container
know whether to call doGet
or doPost
method.
When I make a get request doGet
is called, When I make a post request doPost
is called , but where is the logic to decide this .
The logic is in the HTTP protocol and its management by the servlet container (Tomcat, Glassfish, Jetty, ...)
The first word of the request (at the TCP level) is the HTTP verb, generally GET or POST but it can be DELETE, PUT, OPTIONS, HEAD, TRACE,...
The servlet container call the
service
method of the servlet, but the default implementation ofHttpServlet.service
method contains the logic to dispatch to the proper method. Extract from the Javadoc :Dispatches client requests to the protected service method. There's no need to override this method.
Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class. This method is an HTTP-specific version of the Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse) method. There's no need to override this method.
Request Method is a standard HTTP/1.1 token, which is sent as part of request headers
Please refer to:- http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
You never really call
doGet()
ordoPost()
(the service() method will, and it is called by the Web container as you read in the lifecycle).The
service()
method detects the HTTP method used and delegates todoGet()
,doPost()
and other methods which process HTTP requests in aHTTPServlet
. It also encapsulates theServletRequest
andServletResponse
objects inHttpServletRequest
andHttpServletResponse
objects which contain additional context data from the HTTP headers.Tahnks to @helderdarocha.
For more;
javax.servlet.http.HttpServlet.service(HttpServletRequest req, HttpServletResponse resp) contains the logic for that.