[Servlet articles] Introduction to servlet related classes

Table of contents

 When is the servlet object created?

2. The role of each method in the servlet interface

3. Introduction of related classes and interfaces

GenericServlet

ServletConfig

ServletContext

HttpServlet


 When is the servlet object created?

1. Under normal circumstances, when tomcat starts, it does not create a servlet object immediately, but when the client accesses the mapping directory configured by this servlet for the first time, tomcat constructs a servlet object, calls the init method, and executes the service method at the same time . If you visit later, the init method will not be executed, but the service will be called directly, and the destroy method will be called when tomcat is restarted or shut down normally. Then, init is used by the client for the first visit, and service is used by the client every time it visits, and it is necessary to know that a new thread is created every time the service method of the servlet object is called. destroy is called when tomcat shuts down normally.

2. In special cases, if we configure load-On-Startup in the XML file, then the Servlet object is created when tomcat starts.

From the above description, we can know that the Servlet is only created when it is accessed for the first time, and it will not be created later, so the Servlet is a singleton. Servlet is a single instance of multithreading. But it is a false singleton, because the singleton mode stipulates that the no-argument construction method must be abstract, and the outside world cannot directly access the construction method, and needs to be obtained from the specified method provided by us;

2. The role of each method in the servlet interface

method name     method description
init(ServletConfig config)   The initialization method of the servlet instance
ServletConfig getServletConfig Get the configuration information when the servlet instance starts
service(ServletRequest req, ServletResponse res)  Handle requests from web clients
getServletInfo()  Get the service information of the servlet itself
destroy();     The destruction method called when the servlet is destroyed by the web container

3. Introduction of related classes and interfaces

 

GenericServlet

Why is there a GenericServlet?

        When we usually write a servlet demo, we need to implement the servlet interface. To implement the servlet interface, we must rewrite the five methods in the servlet interface. However, among these five methods, we actually use only one service() method. Every time we implement the servlet It is too troublesome and cumbersome to rewrite these five methods for the interface, so we created an intermediate class GenericServlet, let it implement the servlet interface and rewrite the five methods, and then defined the main method service() method as abstract method, the class is defined as an abstract class, so that we can only inherit the GenericServlet class when writing the servletdemo class, and only need to rewrite a method service(); this involves a design pattern (adapter design pattern);

ServletConfig

        After we write a servletdemo class by hand, we will configure relevant information in the XML file, so how can we obtain this information in subsequent applications? At this time, the ServletConfig interface is needed. All configuration information of the servlet object is stored in the ServletConfig interface. How to understand it? After we write a servletdemo class by hand, we will configure relevant information in the XML file, and ServletConfig stores the information in the tag <servlet></servlet> in the XML file;

        Every time we write a servletdemo class under the same project, we must configure relevant data in the XML file once, so that each servlet corresponds to this servletConfig interface; servletConfig does not need to be created by ourselves, it is created by Created by the Tomcat server;
        what are the common methods of the ServletConfig interface?

      public String getInitParameter(String name); // 通过初始化参数的name获取value
      public Enumeration<String> getInitParameterNames(); // 获取所有的初始化参数的name
      public ServletContext getServletContext(); // 获取ServletContext对象
      public String getServletName(); // 获取Servlet的name

        The above methods can be called using this in the Servlet class. Because GenericServlet implements the ServletConfig interface.

ServletContext

  • A Servlet object corresponds to a ServletConfig. 100 Servlet objects correspond to 100 ServletConfig objects.

  • As long as they are in the same webapp, as long as they are in the same application, all Servlet objects share the same ServletContext object.

  • The ServletContext object is created during server startup and destroyed when the server is shut down. This is the life cycle of the ServletContext object. The ServletContext object is an application-level object.

  • There is a webapps in the Tomcat server, the webapps can be stored under this webapps, and multiple webapps can be stored, assuming that there are 100 webapps, then there are 100 ServletContext objects. But, in short, an application, a webapp must have only one ServletContext object.

  • ServletContext is called Servlet context object. (The surrounding environment object of the Servlet object.)

  • A ServletContext object usually corresponds to a web.xml file.

  • What examples in life does ServletContext correspond to?

    • There are multiple students in a classroom, then each student is a Servlet, and these students are in the same classroom, then we can call this classroom a ServletContext object. So that is to say, the data placed in this ServletContext object (environment) is shared in the same classroom. For example: there is an air conditioner in the classroom, and all students can operate it. It can be seen that the air conditioner is shared. Because the air conditioner is placed in the classroom. The classroom is the ServletContext object.

  • ServletContext is an interface, and the Tomcat server implements the ServletContext interface.

    • The creation of the ServletContext object is also done by the Tomcat server. Created when starting the webapp.

HttpServlet

We know that when we write a servlet, we don't need to implement the top-level interface Servlet, we only need to inherit HttpServlet and implement the doGet and doPost methods.

The proposal of the Servlet interface is to solve the data processing based on the request-response model, and does not involve the HTTP protocol and related APIs. Related APIs.

Guess you like

Origin blog.csdn.net/m0_64231944/article/details/128757900