As the saying goes, the ruler is short, but the inch is strong. Compared with Python, which leads the field of machine learning and scientific computing, Java is more focused on back-end development, occupying half of the country under deep cultivation, which cannot be separated from the advantages of language and framework support.

The most natural way to master the Java Web framework is to take it one step at a time, following technological milestones; Where the framework code is involved, it is also simplified, using imitation code to simulate the framework operation mechanism;

1. Pre-knowledge

1.1 reflection

Typically, calls to a Class Member must be made through its object instance or Class (static methods). Reflection takes a different approach, allowing developers to access its members through code, providing a variety of granular representations of Java objects.

1.2 annotations

Annotations are similar to Interface, which mainly serve as identification and parameter transfer and decouple from business logic.

It can be meta-annotated DIRT annotated, and DemoAnnotation in the example is annotated by Target to indicate that it applies to method types;

2. The Socket and Servlet

2.1 the Socket

Naturally, Web applications cannot do without the support of the underlying network. The transmission of network data mainly consists of two parts: Socket and network protocol stack (implemented by the operating system). The complexity of the stack is handled by the computer network.

Java provides network programming support through the Socket API. As a developer, you mainly work with sockets. A pair of connections is represented by a Socket pair, and the Socket identifier is a binary :(port, address);

A simple dummy Server can be done with the following code:

2.2 the Servlet

Servlet is an interface specification for Java Web development, which adds the mapping between URL and Java classes **(URL, Servlet) on Socket basis, similar to: \resource\list -> com.alan.XX**;

Servlet has no other magic except realizing the Servlet interface. It cannot independently serve Web requests. It must cooperate with Servlet containers to play its real role.

Servlet container is responsible for Servlet registration, loading, service, can be configured and reflected to achieve a simple Servlet container, code address: XX;

  • Monitor the service address, obtain the network connection, and convert it into Request and Response according to Http protocol

  • Query configuration:

    • Load the corresponding Servlet implementation class through reflection and call the Service method
    • If no CORRESPONDING URL is configured, 404 is returned

3.SpringMVC

SpringMVC abstracts the mapping between URL and Servlet in the Servlet specification as HandlerMapping ** (URL,handler), and adds a new mapping layer on this basis ViewResolver** (view,html_etc).

In HandlerMapping, there are two types of mapping results:

  • If the match is met, the request is processed by the corresponding Handler

  • Mismatch

    • If there is a default handler, use the default handler
    • Back to 404

SpringMVC is based on the Spring framework and simplifies Web development through annotations. The dispatching class is DispatchServlet, which is registered to the Servlet container and responsible for intercepting requests and distributing them.

3.1 configuration

The Tomcat configuration file web. XML is as follows. Configure preloading DispatchServlet and passing the configuration address through init-param

Application. properties simply sets the path for packet scanning

scanPackage=learn.web.springmvc.controller
Copy the code

3.2 the initialization

  • 1. When Tomcat loads the DispatcherServlet, it calls its init method and starts loading the configuration according to the init-param parameter and the path of the configuration file.

  • 2. Scan for packages. Concatenate the package name with the file name to obtain the class full path name and add the package name to the class name list

  • 3. Use the reflection mechanism to instantiate
  • 4. Handle annotations, initialize HandlerMapping, first check whether the class has Controller annotations, then check whether the method has RequestMapping annotations, concatenate the URL, and register the configuration items

3.3 Request Distribution

  • Handle requests according to the handlerMapping configuration

2. Parameter parsing retrieveParams is responsible for parameter parsing and corresponding assignment (mapping between request parameters and method parameters), mainly through reflection mechanism, which is simply matched by Type.