Servlet interface consist of service method and also Httpservlet contains its own specific service method. During servlet life cycle public service method of HttpServlet is invoked after init method. If so when the protected service method of Httpservlet class is invoked?
I am in a bit confused between these two service methods. Please clarify me
The Servlet interface defines the methods that must be implemented by all the servlets and the HttpServlet is an abstract class that needs to be extended(inherit) to create a HttpServlet suitable for a web application(website or online application).
The Servlet container always call the service method defined in the Servlet interface. The HttpServlet implementation of this service method just call the service mehtod with HttpServletRequest and HttpServletResponse.
You can see the definition of the methods in
You can see more here
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html
For an inbound request the container calls the service method of the target servlet:
The target servlet could be implemented in several ways:
See: https://docs.oracle.com/javaee/7/api/javax/servlet/Servlet.html
See: https://docs.oracle.com/javaee/7/api/javax/servlet/GenericServlet.html
Note that 3 is the recommended method to use because the default service method will call it's protected version which then handles the inbound method and calls doGet, doPost, doHead etc as appropriate.
see: https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServlet.html
javax.servlet.Servlet
is an interface which is implemented byjavax.servlet.http.HttpServlet
. That means the methods ofServlet
are never invoked; an interface just defines an API - there is no implementation behind it.The servlet life cycle will make sure that first the
init()
method ofHttpServlet
will be called. When requests come in that match the servlet's URL, the container will callHttpServlet.service()
which distinguished between the various HTTP types (GET
,POST
, ...) and call the correct handler method (doGet()
,doPost()
, ...).[EDIT] You should read up on the difference between Java interfaces and classes.
Maybe it's easier to understand when you see some code:
The
...
means "something happens here but it's not important to understand the example".A real servlet is created by extending
HttpServlet
. This class implements theServlet
interface. That means the assignment works. The compiler will use the methods defined inServlet
to find out whetherservlet.init()
is valid. But at runtime, the methodHttpServlet.init()
will be called.Servlet is an interface. So, it has no implementation. If you see Servlet.service() call, actually the implementation in HttpServlet class is being called. If you check HttpServlet class, It has 2 service methods. First is the implementation for the one in the Servlet Interface public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
This method just calls the internal method protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException.