1. Basic Concepts

1. CS and BS architecture

CS architecture pattern

In Client/Server mode, both server-side programs should be written and Client programs should be developed. When software is updated, both Client and Server sides need to be updated. Compared with BS architecture, the overall mode is more complex, but with higher security.

B/S architecture pattern

That is Browser/Server (Browser/Server), only need to write server-side programs, the Browser interface as access to the Server entrance, the architecture is relatively simple, can quickly iterate, but the security is poor.

2. Socket communication mechanism

TCP/IP protocol

Transmission control protocol (TCP)/Internet protocol (IP) refers to a cluster of protocols that can transfer information between different networks. TCP/IP is not only TCP and IP, but a protocol cluster consisting of FTP, SMTP, TCP, UDP, and IP. It is called TCP/IP because TCP and IP are the most representative of TCP/IP.

The Socket Socket

An abstraction of an endpoint for two-way communication between application processes on different hosts in a network. A socket is one end of process communication on a network and provides a mechanism for application layer processes to exchange data using network protocols. Usually receive request data, and do business processing is called the server ServerSocket, send requests and receive the results of processing is called the client.

Second, Http protocol

1. Http and Https

The Http protocol

HTTP hypertext transfer protocol is used to transfer hypertext from the World Wide Web server to the local browser transfer protocol, based on TCP/IP communication protocol to transfer data: HTML files, images, query data, etc. The HTTP protocol is based on the client-server architecture. As the HTTP client, the browser sends requests to the server, namely the WEB server, through the URL. According to the received request, the Web server processes the request and sends response information to the client.

Protocol features: simple, fast, flexible, connectionless, stateless, supports client/server mode.

The Https protocol

The SECURity-based HTTP channel is the secure version of HTTP. SSL layer is added to HTTP requests. The SECURITY basis of HTTPS is SSL, so SSL is required for encrypting details. To put it simply, HTTPS is a network protocol that uses SSL and HTTP to encrypt transmission and authenticate identities. It is more secure than HTTP. The HTTPS protocol has two main functions. One is to establish an information security channel to ensure the security of data transmission. Another is to verify the authenticity of the site.

Https is different from Http

Security certificate: Https requires you to apply for a certificate from a CA. Generally, there are few free certificates, and therefore a certain cost is required.

Data transmission: Http is a hypertext transmission protocol. Information is transmitted in plain text. HTTPS is a secure SSL encryption transmission protocol.

Connection mode: Http and Https use completely different connection modes and use different ports, the former 80 and the latter 443.

2. Get and Post requests

The browser

From a browser perspective, the difference between the two types of requests is that GET reads resources, such as static pages, even if multiple reads have no impact on the access data, also known as “idempotent” requests. POST defines a form on a page. Submitting a form submits data to the server, and in most cases produces data, such as the commonly used interface to save data. It is not “idempotent”, which means it cannot be executed multiple times.

The service interface

This refers to the type of request submitted by an Ajax application requesting a service interface. Or other Http request utility classes, or in the case of microservices between various Feign interfaces. In this case, the interface has few restrictions on sending requests. For example, REST-style interfaces commonly use GET, POST, PUT, and DELETE to obtain, create, update, and DELETE resources.

3. Handshake and wave mechanism

Three-way handshake

First handshake: The client initiates a connection request to the server. SYN=1 is SENT in the request packet, and the initial sequence number (SEQ = X) is randomly generated. At this time, the client process enters the SYN-sent state.

Second handshake: After receiving a request packet, the server acknowledges the client’s SYN. If the request is not rejected, the server sends an acknowledgement packet. The packet contains ACK=1, SYN=1, ACK= X +1, and a SYN packet seq= Y is sent. In this case, the server processes syn-RCVD.

Third handshake: After receiving the handshake, the client needs to confirm the ACK=1 and ACK= y+1 packets to the server. In this case, the TCP connection is ESTABLISHED and the client enters the ESTABLISHED state. After completing the three-way handshake, the client and server begin transferring data.

Four times to wave

First wave: The client sends an end FIN to actively close data transmission with the server, release the connection, and stop sending data. The packet header is FIN=1, and the serial number is SEq = U. Then the client enters the fin-Wait-1 state.

Second wave: When the server receives the FIN, it sends an ACK packet (ACK=1) with the received SEQUENCE number +1 (ACK= U +1) and its sequence number (SEq = V). Like the SYN, one FIN occupies one sequence number. In this case, the server notifies the application process that the client has no data to send. If the server sends data, the client still needs to receive it. This state lasts for a period of time, and the server enters close-wait state. After receiving the confirmation request from the server, the client enters the fin-WaIT-2 state and waits for the server to send a connection release packet.

Third wave: The server sends a connection release packet FIN=1 and ACK = U +1 to the client. The server is still in the half-closed state and may send some data with the serial number seq= W. In this case, the server enters the last-ACK state and waits for the client’s confirmation.

Fourth wave: After receiving the connection release packet from the server, the client sends an acknowledgement with ACK=1, ACK= W +1 and seQ = U +1. In this case, the client enters the time-wait state. In this case, the TCP connection is not released. The TCP connection enters the CLOSED state after the maximum lifetime of the packet segment. MSL: indicates the maximum lifetime of a packet segment, usually 2 minutes. When a TCP connection is released, the active party must go to the CLOSED state after 2MSL. Therefore, the active party is CLOSED late.

Servlet component

Java written server-side programs, with platform – and protocol-independent features, the main function is to interactively browse and generate data, generate dynamic Web content. With servlets, you can collect user input from web forms, render records from databases or other sources, and create web pages on the fly.

1. Implementation method

Inherit HttpServlet, which acts as an abstract template. Template methods: service();

Inherit the GenericServlet abstract class, where the Service method is abstract;

Servlet interface, including init, getServletConfig, Service, getServletInfo, destroy several core methods;

2. Life cycle

Load and instantiate, initialize init, service service, destroy: destroy.

3. Core API components

ServletConfig: Gets the servlet initialization parameters and servletContext object;

ServletContext: Sharing data between dynamic resources throughout the Web application;

ServletRequest: Encapsulates Http request information and is created at request time;

ServletResponse: Encapsulates Http response information and is created at request time;

4. Forwarding and redirection

Forwarding: the control of page hopping on the server side;

Redirection: the server responds to the redirect message and the browser redirects the page.

5. Cookies and Sessions

The Cookie mechanism

Cookie in HTTP is usually used to identify the user’s identity, and the data stored on the user’s local terminal for session tracking. Generally, the data will be encrypted and stored temporarily or permanently by the user’s client computer. The structure is a key and a value. As the server-side response is sent to the client browser. The client browser then saves the Cookie and sends it back to the server the next time it accesses the server.

The Session Session

Variables stored in the Session object are not lost as the user jumps between Web pages of the application, but persist throughout the user Session. Servlets can store data that needs to be shared within a session to HttSession objects. Four domain objects: PageContext, ServletRequest, HttpSession, ServletContext.

6. Monitor. Filter. intercept

The listener

JavaWeb has three components: Servlet, Listener, Filter. Listener refers to the component that listens to the state change of the related object in the application program.

The filter

When the client requests a Servlet, the relevant Filter is executed first. If the Filter passes, the Servlet executing the request is inherited. If the Filter does not pass, the Servlet requested by the user is not executed. Filters can dynamically intercept requests and responses.

The interceptor

The Interceptor in the Spring framework is similar to the Filter in servlets. It is mainly used to intercept user requests and process them accordingly. For example, the interceptor can verify permissions, log request information, and determine whether users log in. Request forwarding does not intercept or filter. Redirects perform blocking and filtering.

Database connection pool

1. C3P0 connection pool

C3P0 is an open source JDBC connection pool that allows applications to automatically reclaim idle connections by initializing database connections based on the C3P0 configuration.

Druid connection pool

Druid connection pooling is built for monitoring, with powerful built-in monitoring features that do not affect performance. The Druid connection pool has a built-in monitoring page that provides very complete monitoring information to diagnose system bottlenecks. It is also the most commonly used connection pool.

5. Run the server

1. Jetty containers

Jetty is an open source servlet container that provides a runtime environment for Java-based Web containers such as JSPS and servlets. Jetty is written in the Java language, and its API is distributed as a set of JAR packages. Developers can instantiate the Jetty container as an object that can quickly provide network and Web connectivity for some standalone Java applications.

2. Tomcat server

Tomcat server is a free open source Web application server, belongs to lightweight application server, in small and medium-sized systems and concurrent access users are not many occasions is widely used, is the first choice for developing and debugging JSP programs. The two most critical operations to use Tomcat are connecting to Tomcat using development tools and deploying web applications. Package the application to run under the Tomcat service.