Detailed principles underlying the development of Java Web applications

Foreword

Previous article, I briefly chatted we are now commonly used in the design basis and principles of the Spring framework, mainly inversion of control and dependency injection, and programming concepts such as container from the relationship as a whole and with the operating environment of the entire application .

Here I do not want to repeat the definition of these concepts, because those things online just to be able Baidu, I want my description of these concepts together, so that we better go immediately know why they do so, we have developed a day framework used in the end what is the origin of its design and specifications. Also we do know these know why, allow us better to use them in the development process, know that it probably face the problem solving orientation.

In this paper I want to continue along the front to talk about the idea of ​​Web-based applications require the use of container management Spring Framework related to the development of understanding.

Web applications and Servlet specification

Of course, talking about application development, we are all familiar with, and now there are many classification applications, the initial console program, program service components, the desktop application to a web-based HTTP access protocol should be procedures.

In fact, their program is essentially based on some input / output processing process. For example, our most common but rarely practical application console application that is standards-based I / O to achieve application class, receiving a command line as an input stream, the console output as a standard application form. It runs just to have a process to build the shell input and output streams can be.

For Web applications we want to talk in detail today, in fact, it is originated to one run on an operating system component programs, but their data input and output is based on the network data stream.

Networking Basics

From the network knowledge base we know, need to transfer data over the network via a 7-layer model, that is, from the initial abstract definition of network hardware to the most advanced applications from penetrating this level.

Data To connect two physical machine, we need to identify two machine name, and those relying on the IP port and the transmission on the network link is the byte stream, to know that these data streams are not What specific format, but when the network layer, we need to know where it came from to be sent to whom, so we need to be certain format restrictions, this abstraction is done by telegram defined format.

For example, we need to define the length of the transmission, labeled, if sequential, etc. These can be wrapped byte stream into a data packet, then we must define the agreement between each transmitting end and receiving end, it is to tell the other side I What is transmitted, how do you receive it, such as how long is a complete packet, packet sequencing, etc., these are the two we know the IP address of the communications shop, and how to connect the transmission is what we say control protocol (TCP) on the basis that we define higher-level application protocols such as HTTP, FILE, MAIL and other agreements, of course, the most common protocol is the HTTP protocol.

HTTP protocol

It is a higher level of abstraction in the message format defined on the basis of the basis, we can tell they are willing to resolve data communication. Take for instance the Hypertext Transfer Protocol, which defines the header information and content information, it also provides for a method of processing information and feedback the results of the code, this is what we often say that the GET, POST, DELETE, OPTIONS, etc., return code such as from 100 to 500 series, of course, these protocols have been entered into the application part.

All of our applications are point-to point network based communications. That is to create such a program we must first identify the two points can be connected, first of all is the host name or IP address, then that particular application we want to connect, it will normally be reflected in the following which port or ports which on the path. With the definition describes peers, we can define its control transfer abstract concepts socket.

Codec problem

It is essentially to create a stream of input output channel, network traffic is to define and describe the concept by Socket. And our programming, especially in Java programming, we only need to define a channel can be connected to a network Socket, and set aside a buffer in memory, so that the channel can have a workable space in the application space we managed, then use the process flow of data between different buffers the data corresponding change in the form, for example, the most basic is how a stream of bytes transmitted over the network, into a high-level language we define advanced data types of the process, this process is often referred to to decode, also when various types of data we need to be able to be understood that the application to be used into a byte stream elsewhere, this process is referred to as a coded transmitted over a network.

Because the current hardware will be able to get to know the two states, which two states is represented by binary numbers 0 and 1, so many high-level programming language we use, the complex data types which need into byte binary form it can be understood CPU, network hardware being transmitted. So our programming inevitably need to complete this encoding and decoding processes. Of course, with the continuous evolution of high-level language, commonly used in various types of treatment it has been transformed into a variety of languages ​​standard library function or package, we just need to make use of them. Unless you want to write your own communication protocol or define a special data format, or for the codec is generally not involved.

Now that you know all applications are treated on the basis of this data stream, we will look at Web application that is based on access to services and applications independent of the network structure, that is, we often say that the Server-Client mode.

About Servlet

The reason here define the server and the client in fact, mainly a functional distinction, but in essence, is the bottom two computers connected by a byte stream to exchange data, and data to a predetermined transmission control protocol decoding, and our web applications is web-based applications HTTP protocol, the network processing involved, the art relevant network processing portion independent predetermined many specifications, such as endpoint Descriptors, data transmission format specification, how to use the host computer operating system norms set by the environment, etc. these reactions to programming in Java is that we are all familiar with the Servlet specification.

This specification tells us first of all based on the underlying network portion of Web-based applications need to have a role on each networked computer to be responsible, we call this role as a container, or a web server.

It is to achieve identification of the computer network, connection and is specified analytical data format, of course, then we will be developed into a comprehensive server side to handle the HTTP protocol, while also operating system more interactive call operation via a number of interfaces functional components of the system to handle. Such as network cards, file input and output controllers, and so on.

We can describe a simple function to achieve a Servlet container, first of all it needs to have an abstract host information to run their own, can let the program run its understanding and use of resources that it can use.

Then, it needs to be advanced based on data types of byte stream conversion network, such as the byte-parsed into a data format follows the HTTP protocol, HttpRequest, HttpResponse and HttpServletRequest, HttpServletResponse like.

While for the host server environment parameter abstraction is introduced into the container for interacting with our application. So long as the implementation of the Servlet specification, it can be used as an intermediary between the operating system and our applications.

Servlet container

There are many mature products Servlet container such as Tomcat, Jetty, Weblogic, Glassfish, etc. on the market. There are a lot of lightweight, network traffic is only responsible for converting the input can be understood and processed data in the form of our application, and this process is through the creation process input and output data streams to complete. There are some additional business applications to achieve more content, such as inheritance of abstract system of environmental resources, such as database connection resources, file input and output components.

We develop applications based on the principles of our design and development, we can first use an exploded assembly function, the function of each component may be designed as a separate component run in a container, the assembly is HttpServlet request processing component.

We will be labeled according to the request destination address various functions, then these unique destination address and HTTP methods to identify the target component running, and this component can interact with the computer through the container to the environment.

So the top layer is a Servlet abstract service method, the input parameters of the method is carried out through the package body and the revertant request and environment variables like objects by the container.

Of course, we will come to support their specific refinement of the method according to the HTTP protocol HTTP, so we can be done by specific processing doGet, doPost methods.

With our basic function of such a specification to achieve, we have a container can accommodate and manage specific functions of application components, the container is what we call web server.

If you know the nature of the Servlet specification it is to package and codec processing network data stream, you can base yourself from packaging and codec converts the binary data stream to start designing your own web application server.

That is, to achieve point to point communication processing, said here about re-factoring basis of the current micro-services architecture is the foundation for the web server networks design.

Servlet 3.0 introduces the concept of a flow reactor, is controlled by the recipient to manage large quantities of data input and output streams.

Spring Framework and Web Application Design

After understanding about the structure of Web applications above, let us look at what Spring Framework play in web application development roles yes.

We know that Java enterprise development have Java EE framework, in fact, based on Servlet container, it's just the enterprise application all the basic functions of the development of all components of the, such as a container of dependency injection, JPA, of course, must have a matching Web application server to support its operation.

The same core of the Spring framework is a container component, its function is more effective through more lightweight application to organize and manage various functional components.

Its clever is that the entire assembly is designed as a Servlet component implementation, which is the core of the Spring framework DispatchServlet, with all the Servlet specification is defined as a target path we need to identify the Servlet request, then let the Servlet container it loads at startup, and is bound to the target path, in order to start an application container component management processor a request to the root directory, and a processor which is implemented as a front end to control mode handler responsible for after which the root portion of the URL identified and matched in order to achieve route processor is responsible for the processing of subsequent Spring container resource URL.

Simply put, that is, when the external access request via the network to the Web server, Servlet specification and will be based on the HTTP protocol decode it into HttpServlet request and response data structure type, and then parse the destination URL of its access to resources, to match us Spring container registered for processing its components and the method name to complete the processing of the request Servlet.

Now that we have when developing applications in addition to the continuous process of uploading and downloading files, but mostly to convert binary data format JSON or XML format, so we only need to register the corresponding processing components can be in the Spring container.

to sum up

Here to say things have not finished, but the length of articles has exceeded expectations, so stop right now, can only open up in the next article went on to speak of.

This article simply talked about the characteristics of Web applications, and can assist Web applications that run base container service standards, and then to the design principles and structure of the Spring framework to achieve the design.

Here we hope to bring the Web application is different from the characteristics of other types of applications start to support Web applications running Servlet specification implemented in the Spring application framework plays a role in the Web application content such as over again . Next, I will continue along this line of thought, talk about the content of the MVC pattern, and reaction stream processing mode.

Published 37 original articles · won praise 470 · views 120 000 +

Guess you like

Origin blog.csdn.net/weixin_42784331/article/details/104486693