java web basics

1. What is Servlet

        A servlet is essentially an interface, and it doesn't care about any network protocols, such as http. What is the role of the servlet interface? specification!

        The servlet interface defines a set of specifications for processing network requests. All classes that implement servlets need to implement the five methods, the most important of which are the two statement cycle methods init() and destroy(), and one The service() that handles the request. In other words, all classes that implement the servlet interface, or in other words, all classes that want to process network requests, need to answer these three questions: ( things to be done in the specification)

  • What do you do when you initialize?
  • What do you do when you destroy?
  • What do you do when you receive a request?

        Servlet is a specification, so can a class that implements servlet handle requests? cannot. You never write any code that listens to port 8080 in the servlet, and the servlet will not directly deal with the client!

        How did the request come to the servlet? The answer is the servlet container, such as our most commonly used Tomcat . servlets are deployed in a container, otherwise your servlet will not work at all.

        Tomcat is the guy who directly deals with the client. It listens to the port. After the request comes, it determines which servlet to process the request according to the URL and other information, and then calls the service method of that servlet. The service method returns a response object. , Tomcat returns this respond to the client.

So what is a servlet container? The servlet container, as the name implies, stores servlet objects .

1. The client requests the Servlet;

2. Tomcat loads the Servlet class into memory;

3. Tomcat instantiates and calls the init() method to initialize the Servlet; (tomcat only recognizes the init() method, all need to inherit the method of the parent class or create the method)

4. Tomcat calls service() (call doGet() or doPost() according to different request methods, in addition to doHead(), doPut(), doTrace(), doDelete(), doOptions()); (tomcat only recognizes xxx () method, all methods that need to inherit from the parent class or create this method)

5、destroy();

6. Load and instantiate the Servlet. This operation is generally performed dynamically. However, the Server usually provides a management option to force loading and initialization of a specific Servlet when the Server starts;

7. Server creates an instance of Servlet;

8. The first client's request arrives at the server;

9. The server calls the init() method of the Servlet (it can be configured to be called when the server creates a Servlet instance, and the <load-on-startup> tag is configured under the <servlet> tag in web. Smaller Servlets have higher startup priority);

10. A client's request arrives at the server;

11. Server creates a request object to process the client request;

12. The Server creates a response object to respond to the client request;

13. The Server activates the service() method of the Servlet, passing the request and response objects as parameters;

14. The service() method obtains information about the request object, processes the request, accesses other resources, and obtains the required information;

15. The service() method uses the method of the response object to send the response back to the server and finally to the client. The service() method may activate other methods to process requests, such as doGet() or doPost() or new methods developed by programmers themselves;

16. For more client requests, Server creates new request and response objects, still activates the service() method of this Servlet, and passes these two objects to it as parameters. Repeat the cycle above without calling the init() method again. Generally, the Servlet is only initialized once (only one object). When the Server no longer needs the Servlet (generally when the Server is closed), the Server calls the destroy() method of the Servlet.

The Servlet container is the running environment of the Servlet program, and it mainly includes the following functions:

  • Implement various interfaces and classes defined by the Servlet specification to provide underlying support for the operation of the Servlet;
  • Manage Servlet classes written by users and objects after instantiation;
  • Provides HTTP service, equivalent to a simplified server.

Guess you like

Origin blog.csdn.net/zr_xs/article/details/128927723