2

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

Akhil Mathew
  • 1,571
  • 3
  • 34
  • 69

4 Answers4

4

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

protected void service(HttpServletRequest req,
                       HttpServletResponse resp)
                throws ServletException,
                       java.io.IOException

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.

public void service(ServletRequest req,
                    ServletResponse res)
             throws ServletException,
                    java.io.IOException

Dispatches client requests to the protected service method. There's no need to override this method.

You can see more here

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html

reos
  • 8,766
  • 6
  • 28
  • 34
  • So you mean that web container call Servlet.service() method and it is then call the HttpServlet.service() method? – Akhil Mathew Dec 03 '15 at 17:26
  • Yes, that is in the documentation. Dispatches client requests to the protected service method. There's no need to override this method. – reos Dec 03 '15 at 17:27
  • But other people says that HttpServlet.service() is invoked by container and Servlet.service() is never invoked. See below answers – Akhil Mathew Dec 03 '15 at 17:32
  • In my answer it is in yellow, that is the definition in the official api website – reos Dec 03 '15 at 17:49
  • Thanks for the answer. I was so fed up with this confusion – Akhil Mathew Dec 03 '15 at 18:01
3

javax.servlet.Servlet is an interface which is implemented by javax.servlet.http.HttpServlet. That means the methods of Servlet 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 of HttpServlet will be called. When requests come in that match the servlet's URL, the container will call HttpServlet.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:

Servlet servlet = new HttpServlet(...);
...
servlet.init();
...
servlet.service(...);

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 the Servlet interface. That means the assignment works. The compiler will use the methods defined in Servlet to find out whether servlet.init() is valid. But at runtime, the method HttpServlet.init() will be called.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • When i searched on the internet, every where it is written that implemented method service(ServletRequest,ServletResponse) is invoked after init method. But my doubt is when the service(HttpServletRequest,HttpServletResponse) method of HttpServlet class is invoked? – Akhil Mathew Dec 03 '15 at 16:54
  • @Perdomoff So How the HttpServlet.service() method is invoked? – Akhil Mathew Dec 03 '15 at 17:03
  • @Aaron If the methods of servlets are never invoked then how Servlet.init() method is invoked? You mean that Servlet.service() is never invoked? – Akhil Mathew Dec 03 '15 at 17:15
  • @AKHILMATHEW: `Servlet.init()` can't be invoked since there is no code which could be run. What will be invoked is `HttpServlet.init()`. – Aaron Digulla Dec 04 '15 at 08:43
  • Actually there was a confusion. Sorry for the inconvenience. – Akhil Mathew Dec 04 '15 at 09:33
1

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.

Devendra Bhatte
  • 358
  • 4
  • 18
  • Thanks for you reply . So you mean first implemented service method is invoked and it is then calls the service method of Httpservlet? – Akhil Mathew Dec 03 '15 at 16:43
  • Yes. You can check it with the source code. http://grepcode.com/file/repo1.maven.org/maven2/javax.servlet/servlet-api/2.5/javax/servlet/http/HttpServlet.java#HttpServlet.service%28javax.servlet.ServletRequest%2Cjavax.servlet.ServletResponse%29 – Devendra Bhatte Dec 04 '15 at 14:49
1

For an inbound request the container calls the service method of the target servlet:

protected void service(HttpServletRequest req,
                   HttpServletResponse resp)
            throws ServletException,
                   java.io.IOException

The target servlet could be implemented in several ways:

  1. Implement the javax.servlet.Servlet interface. In this case the servlet must provide an implementation of the service method and it will be called.

See: https://docs.oracle.com/javaee/7/api/javax/servlet/Servlet.html

  1. Extend the javax.servlet.GenericServlet abstract class. This provides some default method implementations but your servlet must still provide and implementation of the service method and it will be called.

See: https://docs.oracle.com/javaee/7/api/javax/servlet/GenericServlet.html

  1. Extend the javax.servlet.http.HttpServlet abstract class. This provides a default implementation of the service method so if you do not override this implementation the service method of HttpServlet will be called.

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

mmulholl
  • 281
  • 2
  • 7