简体   繁体   中英

what is the difference between public service() method and protected service() method of httpservlet

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

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM