Summary of important knowledge points of Java Servlet

Java Servlet is a program running on a Web server or application server. It acts as an intermediate layer between the request from a Web browser or other HTTP client and the database or application on the HTTP server. Although it has been replaced by many mainstream frameworks, these mainstream frameworks are based on Servlet (JSP is outdated), such as the commonly used SpringMVC framework, so it is necessary to understand the underlying principles of Servlet, so as to facilitate the understanding of the SpringMVC framework

One, the life cycle of Servlet

Servlet objects have a life cycle. The so-called life cycle is a set of methods that must be executed when a servlet is started. Each method executed in the life cycle is in order. The life cycle sequence of Servlet is:

  1. Create, call the constructor
  2. Initialize, call the init() method
  3. Service, call the service(...) method
  4. Destroy, call destroy

The so-called life cycle means that these four methods do not require manual intervention and are the four methods that Servlet automatically configures and calls. Just start a servlet. These four methods must be called! !

Question: What is the use of this life cycle for our developers?

Initialize init(), which is used to load some parameters at startup.
Service service(), which is used to receive requests (including doGet\doPost\doHead...)

Note: The service() method is rewritten, doGet\DoPost will be invalid! ! !
Insert picture description here

In fact, rewriting the service() method means that no matter what the request is, it will be intercepted! ! !
Destroy: destroy(): Called when the servlet object is cancelled.

2. Servlet parameter setting

We hope that when the servlet is started, some static properties will be set in web.xml.
Question 1: Why do we need to set attributes in web.xml?
Answer: The purpose is that the initialization parameters can be modified after packaging.
Question 2: How to implement Servlet to configure static parameters in web.xml?
Answer: servlet supports configuration of initialization parameters! !
Step 1: Configure the initialization parameters in web.xml
Insert picture description here
Step 2: Obtain the parameters in the Servlet class
Insert picture description here
3. Set the Servlet to start with tomcat startup

Question: By default, Servlet objects are created after the request comes! ! What if we want
to start the servlet at the moment Tomcat starts ?
Answer: Servlet technology supports configuration to start Servlet objects when Tomcat starts!
Insert picture description here
1: Setting value is 0~5. Priority level. Start from 0 according to the set sequence number to execute the
servlet sequence. If the priority levels are the same, they are executed from top to bottom.
Note: -1 means the startup will not be executed. (Defaults)

Four, Servlet mapping path

Mapping path support, standard path, also supports wildcard (*) path.

  1. Standard path
    a. The standard path must start with /.
    b. The standard path can be one-level or multi-level.
    – The request path only needs to correspond to the mapping path one-to-one. For
    example: request path: http://localhost:8080/web-demo-03-url/user/live
    mapping path: /user/live
    Insert picture description here
    2. Wildcard path
    Servlet supports wildcard path , The wildcard is to use the (*) sign to support, servelet intercepts a range of requests! !
    Requirement 1: Write a servlet to intercept all requests
    Insert picture description here
    Requirement 2: Intercept requests beginning with /user/
    Insert picture description here
    Requirement 3. Intercept requests ending with .do
    Insert picture description here

Five, commonly used ServletAPI description

Application Program Interface, application program interface. The application program interface is not equivalent to the interface in our Java! !
Application program interface means to provide program entry for external use. Including (classes, interfaces, enumerations, annotations, etc.)
such as: JDK basic class library documents, called JDK API help documents. It can be understood as
the help documents for the classes, interfaces, enumerations, annotations and other elements provided by the JDK .

  1. HttpServlet (Servlet class, is used to define the parent
    class of a servlet that receives page requests and then processes business codes )
  2. HttpServletRequest, request, used to obtain request information (such as: path, port, data, etc.)
  3. HttpServletResponse, response, used to output dynamic pages, output streams, redirects, etc.
  4. ServletConfig, Servlet initialization parameter configuration interface, used to obtain the initialization parameters of web.xml

Insert picture description here
to sum up:

  1. Life cycle, the so-called life cycle is a few methods that must be executed after starting a servlet object.
  2. The life cycle is sequential: creation, initialization, service, destruction
  3. servlet startup configuration
  4. Servlet parameter configuration
  5. Configuration of the mapping path of the servlet

Guess you like

Origin blog.csdn.net/qq_41936224/article/details/108831489