www.cnblogs.com/guardian-hb…



0. Basics

Open servlet source code in IDEA:

You can see that the servlet is an interface; An interface is a specification that makes it possible for classes that have some commonality to implement the interface and thus follow certain specifications.

This is not the case. Servlets do not listen on port 8080. Dealing directly with clients is a “container,” such as tomcat, which is commonly used.

The request from the client goes directly to Tomcat, which monitors the port. After the request comes, it determines which servlet to handle the request according to the URL and other information, and then calls the service method of that servlet, which returns a response object. Tomcat returns the response to the client.

1. Servlet life cycle

From creation to destruction:

  1. callinit()Method initialization
  2. callservice()Method to handle client requests
  3. calldestroy()Method to free a resource and mark itself as recyclable
  4. Is collected by the garbage collector

As you can see from the above, the process between the init and destroy methods of a servlet is called the life cycle of a servlet.

The entire process of the call is shown in the figure above.

When a servlet is first called by the request container, init() is initialized,

But when a request is called to a servlet again, the container triggers multiple threads to access a servlet’s service () method at the same time.

As you can see, multiple customers accessing the same service () method have thread-safety implications.

If the service() method does not access Servlet member variables or global resources such as static variables, files, database connections, etc., it only uses the current thread’s own resources, such as temporary variables that do not point to global resources, request and Response objects, etc. The method itself is thread-safe and does not require any synchronization control.

If the service() method accesses a Servlet member variable, but the operation on that variable is read-only, the method itself is thread-safe and does not require any synchronization control.

If the service() method accesses a member variable of a Servlet, and operations on that variable are both read and write, synchronization control statements are usually required.

If the service() method accesses a global static variable, and if the static variable may be accessed by other threads in the system at the same time, and if there are both read and write operations, synchronization control statements are usually required.

If the service() method accesses global resources, such as files, database connections, etc., synchronization control statements are usually required.

Interview question: How does a Servlet handle multiple requests at the same time?

Single-instance multithreading: When a request is made, a thread is pulled from the thread pool by the thread scheduler as the responding thread. This thread may be already instantiated or may be newly created.

By default, the Servlet container handles multiple requests in a single-instance, multi-threaded manner: 1. When the Web server starts (or the client sends a request to the server), the Servlet is loaded and instantiated (there is only one instance of the Servlet); 2. The container initializes servlets by reading configuration files (such as Tomcat, setting the number of threads in the thread pool via servlet.xml, initializing the thread pool via web.xml, initializing each parameter value, etc.). 3. When the request arrives, the Servlet container dispatches Worker threads from its managed Thread pool to the requester via Dispatchaer threads. 4. The thread executes the Servlet’s service method; 5. After the request is complete, the thread pool is put back to wait for invocation. (Note: Avoid the use of instance variables (member variables), because if there is a member variable, it may happen that multiple threads access the resource at the same time, to operate on it, resulting in data inconsistency, thus causing thread safety issues)

It can be seen from the above: first: single instance of Servlet, which reduces the overhead of generating Servlet; Second, multiple requests are responded to by thread pool, which improves the response time of requests. Third, the Servlet container does not care whether the arriving Servlet requests access to the same Servlet or another Servlet, and simply assigns it a new thread. If there are multiple requests from the same Servlet, the service method of the Servlet will execute concurrently in multiple threads. Fourth: Each request is received by a ServletRequest object, and the ServletResponse object responds to the request.

2. Spring

Any Spring Web entry point is a servlet.

Spring frameworks have been popular for a long time. Things happen and are popular for a reason, so why the Spring framework? The reason is to simplify Java development.

At its core, Spring decouples business and system services and eliminates duplicate code through dependency injection, section-oriented programming AOP, and template technology. With AOP, you can decouple application-wide concerns, such as transactions and security, from their application objects.

In the Spring Bean

1) The difference between POJOs and Javabeans:

“Plain Ordinary Java Object”, a Plain Java Object. Mainly used to refer to objects that do not follow a particular Java object model, convention, or framework.

Pojos implicitly refer to those that have private parameters as attributes of objects, and then define the interfaces that get and set methods access for each parameter. Java objects that do not inherit from any class, implement any interface, and have not been invaded by other frameworks.

A JavaBean is a reusable component written in the JAVA language. Javabeans are Java classes written according to a specification, not a technology, but a specification. We summarized many development techniques and tool functions for this specification. Classes that conform to this specification can be used by other programmers or frameworks. Its methods must be named, constructed, and behave according to certain conventions:

  1. All properties are private.

  2. The class must have a common default constructor. That is, a constructor that provides no arguments.

  3. Properties of this class are accessed using getters and setters, and other methods follow standard naming conventions.

  4. This class should be serializable. Implement the Serializable interface.

Because these requirements are largely convention dependent rather than interface implementation, many developers think of Javabeans as POJos that conform to a specific naming convention.

In Spring, applications live in the Spring container, which creates objects, assembles them, and manages their entire life cycle. The Spring container manages the components that make up the application through dependency injection, which creates associations between cooperating components.

2) Bean life cycle

Spring MVC

The running flow of Spring MVC: