Servlet learning summary (1)

1. Servlet interface implementation class

Servlet is an interface under the Servlet specification, this interface exists in the jar package provided under the Http server

In the javax.servlet.Servlet interface of the servlet-api.jar package under tomcat, we can see the content of this interface

package javax.servlet;

import java.io.IOException;

public interface Servlet {
    void init(ServletConfig var1) throws ServletException;

    ServletConfig getServletConfig();

    void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;

    String getServletInfo();

    void destroy();
}

In the Servlet specification, it is believed that the dynamic resource file that Http server can call must implement the Servlet interface

If it is not a dynamic resource file, the server has no right to call it. If it is a valid dynamic resource file, it must be a class that implements the Servlet interface.

The class that implements the Servlet interface, the server (such as tomcat) can call it

2. The writing process of the Servlet interface implementation class

         The first step : Create a Java class to inherit the HttpServlet parent class and make it a Servlet implementation interface class

        Isn't it a valid dynamic resource file to implement the Servlet interface? See the figure below

  

  

 oneServlet inherits HttpServlet HttpServlet inherits  GenericServlet GenericServlet implements the Servlet interface

oneServlet (subclass)

HttpServlet (父类) 

GenericServlet (Grandpa Class) Grandpa implements Servlet, and subclass also implements Servlet interface

Careful will find that both the parent class and the grandpa class are abstract classes. Why should they be abstract classes?

Review the role of abstract classes: reduce the difficulty of interface implementation classes to interface implementation

The abstract methods that do not need to be used in the interface are handed over to the abstract class to complete , so that the interface implementation class only needs to rewrite the methods that need to be implemented  , which obviously reduces the difficulty of the interface implementation class, and only needs to implement the required methods can.

analysis:

There are five abstract methods in the Servlet interface:

import java.io.IOException;

public interface Servlet {
    void init(ServletConfig var1) throws ServletException;

    ServletConfig getServletConfig();

    void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;

    String getServletInfo();

    void destroy();
}

Only the service() method is useful, the other four abstract methods are useless for the interface implementation class

    1. HttpServlet (parent class) looks at the code and finds that none of these five methods are implemented

    2. GenericServlet (grandfather class) check the code and find that the implementation class except the service() method, the other four methods are all implemented

 3. GenericServlet (grandfather class) leaves the service() method behind, which is not implemented

     Step 2: The interface implementation class rewrites the doget() or dopost() method

This is rather strange. The server (such as Tomcat) calls the Servlet interface implementation class according to the Servlet specification: It will help to do the following two steps:

1. Tomcat has the right to create instance objects of the Servlet interface implementation class (legal dynamic resource files, Tomcat has the right to call)

       Servlet one = new oneServlet();

2. The Tomcat server calls the service() method according to the instance object to process the current request

     one.service();

But there is no service() method in this interface implementation class, instead it overrides the doget() or dopost() method

Look at the source code to know that it is so

HttpServlet (parent class) overrides GenericServlet (grandfather class) leaving service() method , and httpServlet abstract class has many request methods, such as dopost(), doget()..., oneServlet (subclass ) Override the doget() or dopost() method

The Tomcat server calls the service() method according to the sample object of the interface implementation class (this method is the parent class HttpServlet) . If there is no such method in the subclass, go to the upper level to find out if there is this method.

The service() method of the HttPservlet abstract class can be seen to obtain the request method initiated by the browser , String method = req.getMethod();

To determine whether it is a GET or POST request, call the doget() or dopost() method of the (oneServlet) subclass

Here is a look at how Tomcat (server) creates the object   Servlet one = new oneServlet()

Here you can see the this keyword , Tomcat creates an interface implementation class object, Servlet one = new oneServlet() is calling the service() method, one . service()

In fact, the design pattern is used here -------- template design pattern

Guess you like

Origin blog.csdn.net/weixin_43725517/article/details/112559310