Novice web development simple summary (1)-what is Tomcat

table of Contents

A web application development

1. What is web application development

2. Development process

3. What is a web application server

Two Tomcat

1. What is Servlet

2. What is a Servlet container

3. Servlet container classification

4. Architecture

(1 Overview

(2)Server

(3)Service

(4) Process

Three summary


As a mobile APP developer, I want to start learning about web development-related content recently, because I haven't touched it before, so I need to start with the simplest concepts. After watching it for a week, I will summarize and sort out some of the things I have seen.

A web application development

1. What is web application development

There are two common development modes for web application development:

  • One is the B/S (Browser/Server) mode: as long as there is a browser, it can be opened, such as the official websites of major PCs;

Because the browser itself has a general http protocol as the communication protocol, the development of communication modules can be avoided. So when you enter a URL address in the browser, the browser will encapsulate the request into an Http request and send it to the server. After the server receives the request, it will encapsulate the response data into an Http response and return it to the browser after processing the data. . Then this url address actually contains the path of a file to be browsed on the server.

  • The second is the C/S (Client/Server) mode: there needs to be a client to handle business logic separately, and the client here is usually some PC installation program.

2. Development process

Early web application development was used to browse static pages. After clicking the link or entering the url address in the browser, the browser will request the static page of the web application server through the Http protocol. After the web server receives the request, it reads the resource identified by the URI and returns the information header to the browser. The browser is responsible for parsing the Html and presenting the result.

Later, not limited to browsing static pages, some dynamic interactions need to be added. Then there is CGI (Common Gateway Interface) to extend the web application server. After clicking the link or entering the url address in the browser, the browser also requests the web application server. After the web application server receives the request, it will start the CGI program to process the user request, and then return the response data to the web application after processing. Server, the web application server encapsulates the data and returns it to the browser in the form of Http response. The browser is responsible for parsing Html and presenting the result.

Later, Servlet components appeared, running on the server side, and managed by the Sevlert container to achieve dynamic interaction. When you click on the link or enter the url address in the browser, the browser also sends a request to the web application server. After the web application server receives the request, it will hand the request to the Servlet container. The Servlet container will load, create, instantiate the Servlet, and call Servlet specific methods are used for processing, and finally the response result will be given to the web application server by the Servlet container. The web application server will wrap this response into an Http response and return it to the browser. The browser is responsible for parsing the Html and presenting the result.

The advantages of Servlet compared to CGI are:

(1) Servlet is a single instance, which can run multiple requests, each request runs in a separate thread, the instance is generally not destroyed after creation; and CGI exists in the request activation process, not the thread, after the service is completed Will be destroyed.

(2) Standard API, supported by more web servers.

(3) Servlet is Java language, and CGI is Perl, Shell Script or C language.

(4) Servlet container provides error handling and security for Servlet.

3. What is a web application server

Web application development is to allow users to access some web resources. These web resources are divided into static resources and dynamic resources. Static resources do not change during the browsing process, such as HTML, CSS, and some fixed logo images; while dynamic resources need to be dynamically changed during the browsing process (of course, these dynamic changes correspond to complex business logic), such as JSP, ASP, PHP, etc.

And publishing these web resources to the outside so that users can access is the web application server, then the user can access the web application server through the IP address, thereby accessing the web resources. Common web application server WebLogic server (BEA company), WebSphere server (IBM company), Tomcat server (Apache company). The first two are charged, the last one is free and open source, and I will gradually learn about this Tomcat server later.

Two Tomcat

Tomcat is a free Servlet and JSP container. With the function of processing Html pages, it is also a Servlet and JSP container. Then the Tomcat server can be divided into two parts: Http server (web server/web container) and Servlet container. The former is mainly used to process static resources, and dynamic requests are delegated to the latter, and the latter is mainly used to process dynamic interactions.

1. What is Servlet

The Servlet component was also mentioned earlier, which is a web component of Java technology. The Servlet component runs on the server side and is managed by the Servlet container. Servlet mainly deals with interactive browsing and modification of data, and generates dynamic web content, which means that it is mainly used to generate business logic. Servlet cannot be run alone, it must be deployed in the Servlet container, which is instantiated and called Serlvet's related methods (doPost/doGet).

Need to follow the Servlet specification.

2. What is a Servlet container

Servlet container is also called Servlet engine (I think it corresponds to Engine in the four components of Tomcat). Used to provide network services for requests and responses. Based on MIME decoding and formatting. Common MIME types are text/html, application/pdf, video/quicktime, application/java, image/jpeg, application/jar, application/octet-stream, application/x-zip.

So the function of the Servlet container is summarized as follows:

(1) Network service. Servlet can communicate with Http server directly, without establishing ServerSocket, servlet can only focus on business logic.

(2) Life cycle management. Responsible for loading, creating, instantiating the Servlet, invoking the corresponding method of the Servlet, and garbage collection.

(3) Multi-thread management. Automatically create a thread for the servlet, after the servlet runs, the thread automatically ends. But still need to consider thread safety issues.

(4) The declaration method is safe. Use xml deployment description file to configure and modify security, no need to hard code into the program.

(5) JSP support. You can convert jsp into java code.

3. Servlet container classification

There are java servers and non-java servers for web application servers. Then the web application server has to handle both static resources and dynamic resources.

So for the Java server, then the Servlet container can be used as an independent web application server. This is the first type of Serlvet container: an independent Servlet container. For example, Tomcat defaults to an independent Servlet container.

So for non-java servers, the Servlet container has the following two ways of working (I think it can be understood as combining the Servlet container with other servers, the Servlet container is used to handle dynamic interactions, and other servers to handle static resources):

The second type of Servlet container: the in-process Servlet container .

The third type of servlet container: the out-of-process servlet container .

For these two types of Servlet containers include web server plug-ins and java containers (because Servlet is a component of java technology, it must run in the java environment).

The operating principle is: when a client request is received, the non-java server receives the request and forwards it to the Servert container. In fact, the web server plug-in obtains control of the request and passes the request to the Java container, and the java container is transferring The request is passed to the Servlet.

The difference between these two Servlet containers:

(1) In-process Servlet container: The web server plug-in opens a JVM in the internal address space of the web server that it cooperates with, so that the java container can load and run Servlet in the JVM; the out-of-process Servlet container: runs in and In the address space outside the web server, the java container runs in an external JVM.

(2) The web server plug-in is passed to the java container: the in-process Servlert container uses JNI; the out-of-process Servlet container uses IPC.

(3) The in-process Servlet container is suitable for single-process and multi-threaded servers, but its scalability is insufficient; the out-of-process Servlet container has better scalability and stability, but the response speed is not as good as the in-process Servlet container.

The default in Tomcat is an independent Servlet container.

4. Architecture

(1 Overview

The Tomcat server is composed of a series of configurable components. The core component is the Catalina Servlet container, which is the top container for all other Tomcat components. You can first look at the conf/server.xml file under the tomcat installation file to get an overview of what components Tomcat has.


<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>
</Server>

From this document structure, you can roughly see an inclusive relationship between Tomcat's configurable components, which will be explained in sub-sections later.

(2)Server

It is the topmost component, which represents the entire Catalina Servlet container. Can contain one or more Services.

(3)Service

The Service contains one or more Connectors and only one Engine, which is mainly to bind these Connectors to the Engine. So the Service can be divided into two parts: one part is Connector, the other part is Container (I think it can be understood as Servlet container). And this container mainly contains four parts: Engine, Host, Context, and Wrapper.

  • 1)Connector

To communicate with the client, the bottom layer establishes a connection with the client through the socket, receives the client's request and returns the response result to the client, and encapsulates the information into a request and response that the Container can recognize and passes it to the Container.

A Service can have multiple Connectors: some Connectors provide the Https protocol, and some Connectors can provide the Ajp protocol, etc. to complete the response to the client's request.

It can be seen from the code layer that a Connector uses ProtocolHandler to process requests. Different ProtocolHandler implementation classes (under the org.apache.coyote package) represent different connection protocols, such as Http11Protocol, Http11NioProtocol, AjpNioProtocol, etc.

Each ProtocolHandler contains an EndPoint, Processor, and Adapter.

EndPoint: Process the network connection of the underlying socket, receive the request sent by the client, and implement the TCP/IP protocol;

Processor: Encapsulate the socket received by the EndPoint into a request to implement the Http protocol;

Adapter: Because the received request does not follow the Servlet specification, the Adapter converts the request into a ServletRequest, adapts it to the Container, and gives it to the Container for specific processing

  • 2)Container

Encapsulate and manage Servlert, and process business logic through Servlet. As mentioned earlier, Container contains four parts: Engine, Host, Context, and Wrapper. A simple step-by-step explanation of the following effects:

  • Engine

Also known as Servlet engine, what I understand is to manage Servlet. There is one and only one Engine in a Service to complete the processing flow of a request. The Engine can receive requests from multiple Connectors. After the Engine has processed it, it finds the corresponding Connector and returns it to the user.

Engine is network processing on top of request and response. That is to say, Servlet only needs to pay attention to the business logic, and how to establish a connection with the Connector, how to receive information from the Connector, and how to find the previous corresponding Connector, etc. are all handed over to the Engine for processing.

Users can implement StandardEngine, but usually do not need to do so.

  • Host

Virtual host, also known as site. The Engine can contain one or more Hosts. Users do not need to customize Host, only need to implement StandardHost.

  • Context

A web application (an application that is composed of a set of Servlets, Html pages, classes, and other resources, and runs on a web server), runs on a specific Host. A Host can contain one or more Contexts, and each Context has a unique path. By default, each folder under the Tomcat configuration webapps is a Context, and this webapps is a Host.

Users do not need to customize Context, only need to implement StandardContext.

  • Wrapper

Each Wrapper encapsulates a Servlet to encapsulate business logic.

These four parts of the Container process the request through the chain of responsibility model. Each part corresponds to this BaseValue, only one current value is processed, and then it will be handed over to the next Value for execution. After the four values ​​are executed, the result can be returned to the Connector, and the Connector returns to the customer through the socket. end.

With so many components introduced above, how do the components communicate in the body? The following figure can answer these questions.

(4) Process

  • 1. When the user enters the address or clicks on the link in the browser, the browser will send a TCP connection request to the Tomcat server;
  • 2. The Tomcat server agrees to receive the request and establish a connection. At this time, a connection is established with the client through the Connector
  • 3. The browser generates a data packet in Http format and sends an Http request to the Tomcat server
  • 4. When the Connector receives the request, it will encapsulate the request into a ServletRequest through its three components EndPoint, Processor, and Adapter mentioned earlier, and send the Container
  • 5. After Engine receives the request, it will find the specific Servlet according to the mapping relationship between url and Servlet
  • 6. After the Servlet has processed the business logic, the Engine will return the processing result to the corresponding Connector before
  • 7.Connector sends the response result to the browser
  • 8. After the browser receives the data, it will parse the data in Http format and present the result to the user.

Simply put:

The browser is responsible for establishing a connection with the web server through the Connector;

The browser sends the request to the corresponding Connector;

After the Connector receives the request, it encapsulates the request and forwards it to the Container;

Container is responsible for finding the corresponding Servlet to process business logic;

Container is responsible for returning the response result to Connector;

Connector is responsible for returning the response result to the browser;

The browser is responsible for parsing into recognizable data and displaying it to the user.

Three summary

After a week of learning, I finally figured out what Tomcat is, a basic request processing process of Tomcat. But in fact, the second and third mentioned in the Serlvet container classification are still not well understood, and the summary is not good. You still need to look for relevant information.

In addition, there is still a little discrepancy between the content learned and the current company project code structure, and I will continue to study later. Come on, and finally have a little understanding of web application development. I didn't really know how this request was sent to the web server before, but now I finally figure it out.

The next article is to understand the simple summary of the novice web development of Xiaobai (two)-what is web.xml

Guess you like

Origin blog.csdn.net/nihaomabmt/article/details/113180885