Java interview summary summary, including Java key knowledge, as well as common open source framework, welcome to read. The article may have wrong place, because individual knowledge is limited, welcome everybody big guy to point out! The article continues to be updated at……

ID The title address
1 Design Mode Interview Questions (Most comprehensive summary of interview questions) Juejin. Cn/post / 684490…
2 Java Basics (most comprehensive interview questions) Juejin. Cn/post / 684490…
3 Java Set interview Questions (the most comprehensive interview questions) Juejin. Cn/post / 684490…
4 JavaIO, BIO, NIO, AIO, Netty Interview Questions (Most comprehensive summary of interview questions) Juejin. Cn/post / 684490…
5 Java Concurrent Programming Interview questions (most comprehensive interview questions) Juejin. Cn/post / 684490…
6 Java Exception Interview Questions (The most comprehensive interview Questions) Juejin. Cn/post / 684490…
7 Java Virtual Machine (JVM) Interview Questions Juejin. Cn/post / 684490…
8 Spring Interview Questions (the most comprehensive interview questions) Juejin. Cn/post / 684490…
9 Spring MVC Interview Questions (The most comprehensive interview Questions) Juejin. Cn/post / 684490…
10 Spring Boot Interview Questions (Most comprehensive summary of interview questions) Juejin. Cn/post / 684490…
11 Spring Cloud Interview Questions (The most comprehensive interview questions) Juejin. Cn/post / 684490…
12 Redis Interview Questions (most comprehensive summary of interview questions) Juejin. Cn/post / 684490…
13 MyBatis Interview Questions (most comprehensive interview questions) Juejin. Cn/post / 684490…
14 MySQL Interview Questions (most comprehensive interview questions) Juejin. Cn/post / 684490…
15 TCP, UDP, Socket, HTTP interview questions Juejin. Cn/post / 684490…
16 Nginx Interview Questions (The most comprehensive interview Questions) Juejin. Cn/post / 684490…
17 ElasticSearch interview questions
18 Kafka interview questions
19 RabbitMQ interview questions (most comprehensive summary of interview questions) Juejin. Cn/post / 684490…
20 Dubbo Interview Questions (the most comprehensive interview questions) Juejin. Cn/post / 684490…
21 ZooKeeper Interview Questions Juejin. Cn/post / 684490…
22 Netty Interview Questions (Most comprehensive summary of interview questions)
23 Tomcat Interview Questions (The most comprehensive interview questions) Juejin. Cn/post / 684490…
24 Linux Interview Questions (Most comprehensive Summary of interview questions) Juejin. Cn/post / 684490…
25 Internet Related interview Questions (the most comprehensive summary of interview questions)
26 Internet Security Questions (Summary of the most comprehensive interview questions)

What is Tomcat?

  • Apache Software Foundation project is a core project, 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.

What is the default port of Tomcat and how to change it

  • The default is 8080

  • Change the port number mode

    1. Go to the conf folder in the Tomcat directory

    2. Go to the conf folder and find the server.xml file

    3. Open the server.xml file

    4. Find the following information in the server.xml file

    5. Change port 8080 on the Connector label to the desired port

How to install Tomcat on Linux

  1. First go to download the Tomcat installation package, gz end (represents Tomcat on Linux)
  2. Upload it to Linux and unzip it
  3. Change the port number, or do not change it. If you want to change it, change it in server.xml
  4. After that, go to your tomcat bin directory and type./startup.sh.

How do I deploy a project in Linux

  • First use Eclipse or IDEA to type the project into a. War package, and then upload it to the Linux server, and then put the project in the Webapps in the bin directory of Tomcat, and restart Tomcat.

Tomcat directory structure

  • /bin: stores the scripts used to start and stop Tomcat
  • /conf: stores the Tomcat configuration file
  • /lib: stores various JAR packages required by the Tomcat server
  • /logs: Stores Tomcat log files
  • /temp: The Tomcat is used to store temporary files
  • /webapps: directory for publishing Web applications
  • /work: Tomcat defends this directory with JSP generated servlets

Similar to Tomcat, publish JSP to run the Web server and those:

  • Resin provides the fastest JSP /servlets running platform. Resin can flexibly choose appropriate development language for the task with the support of Java and javascript. Resin’s advanced language XSL(XML Stylesheet Language) allows separation of form and content.

  • Jetty is an open source servlet container that provides a runtime environment for Java-based Web content, 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.

  • WebLogic BEA WebLogic is a Java application server used to develop, integrate, deploy, and manage large-scale distributed Web applications, network applications, and database applications. Bring the dynamic capabilities of Java and the security of Java Enterprise standards to the development, integration, deployment, and management of large-scale web applications.

  • Jboss is an open source application server based on J2EE. JBoss code is licensed under the LGPL and can be used free of charge in any commercial application. JBoss is a container and server that manages EJBs and supports the EJB 1.1, EJB 2.0, and EJB3 specifications. The JBoss core service, however, does not include a WEB container that supports servlets/JSPS and is typically used with Tomcat or Jetty bindings.

How is Tomcat optimized?

  1. To change the maximum number of threads for Tomcat, modify the configuration file in the conf/server. XML file: maxThreads= “150” //Tomcat uses threads to handle every request received. This value represents the maximum number of threads that Tomcat can create. Default value: 200. Can be adjusted according to the machine’s performance and memory size, generally can be 400-500. The maximum can be around 800.

  2. Catalina. bat for Windows and catalina.sh for Linux. JAVA_OPTS= ‘-xMS256m -XMx512m’ -xms Size of the HEAP initialized by the JVM -Xmx Maximum heap size of the JVM The actual value is large

What are the tomcat Connector operating modes (optimizations)?

Below, let’s take a look at the three operating modes of Tomcat Connector.

  1. BIO: Synchronizes and blocks a thread to process a request. Disadvantages: When the concurrency is high, the number of threads is large, which wastes resources. Tomcat7 or below, this mode is used by default on Linux systems. Configuration item: protocol= “HTTP/1.1”

  2. NIO: Synchronous non-blocking IO takes advantage of Java’s asynchronous IO processing to process a large number of requests through a small number of threads. Multiple Connections can be multiplexed using the same thread.

    Tomcat8 uses this mode by default on Linux. Tomcat7 must modify the Connector configuration to start. Preparation: protocol = “org. Apache. Coyote. Http11. Http11NioProtocol” remark: we often use the Jetty, Mina, ZooKeeper is based on Java nio implementation.

  3. APR: Apache Portable Runtime solves IO blocking problems at the operating system level. AIO mode, asynchronous non-blocking IO(Java NIO2 also known as AIO) the main difference from NIO is the underlying difference of the operating system. An analogy can be made: compared to express delivery, NIO is to check whether the express has arrived on the official website after online shopping (it may be many times), and then go to pick up the express. AIO is when a Courier delivers to your door (regardless of progress).

    Preparation: protocol = “org. Apache. Coyote. Http11. Http11AprProtocol” note: need to install APR library in local server. Tomcat7 or Tomcat8 starts in this way by default on Windows 7 or above. If Linux has APR and Native installed, Tomcat supports APR when starting directly.

How many deployment modes can Tomcat be used?

You can deploy Web applications in Tomcat in the following ways:

  1. Take advantage of Tomcat’s automatic deployment.

    Copy the Web application to the Webapps directory. When Tomcat is started, it loads the applications in the directory and puts the compiled results into the Work directory.

  2. Deploy using the Manager App console.

    Click “Manager App” on the Tomcat home page to enter the application management console and specify the path or WAR file of a Web application.

  3. Modify the conf/server. XML file deployment.

    Modify the conf/server. XML file to add the Context node for deploying applications.

  4. Add custom Web deployment files.

    Add the xyz. XML file to the conf/Catalina/localhost/ directory, which contains the Context node for deploying applications.

How does the Tomcat container create instances of servlet classes? What principles are used?

  1. When the container starts, it reads the web.xml file in all web applications in the Webapps directory, parses the XML file, and reads the servlet registration information. The servlet classes registered in each application are then loaded and instantiated by reflection. (Sometimes instantiated on the first request as well)
  2. Add 1 to the servlet registration if it is positive, it is instantiated at the beginning, if it is not written or negative, it is instantiated the first time.

Tomcat Working Mode

  • As a servlet container, Tomcat works in three modes:

    • 1. Standalone servlet containers, which are part of the Web server;
    • The servlet container is implemented as a plug-in for the Web server and a Java container. The Web server plug-in opens a JVM in the internal address space to allow the Java container to run internally. Fast response but poor scalability;
    • The servlet container runs in the address space outside the Web server, and acts as a combination of the Plug-in for the Web server and the Java container implementation. Reaction time is not as good as in-process but scalability and stability are better than in-process;
  • Requests to Tomcat can be classified into the following two categories according to the working mode of Tomcat:

    • Tomcat as an application server: Requests come from front-end Web servers, which could be Apache, IIS, Nginx, etc.
    • Tomcat as a standalone server: requests come from the Web browser;
  • It is not common to be asked questions about Tomcat in an interview. Because of this, many people neglect to master skills related to Tomcat. The following article summarizes the system architecture related to Tomcat and introduces the relationships among Server, Service, Connector and Container. The function of each module, it can be said that these several master, Tomcat related interview questions you will not have any problems! In addition, in the interview, you should consciously or unconsciously to Tomcat this place cite, such as the common Spring MVC execution process, a complete URL call link, these related topics you can go to Tomcat processing request this process to say! With these Tomcat skills, the interviewer will admire you!

  • Here’s what you should know after this chapter:

    • The relationship between Server, Service, Connector, and Container, and their main function points;
    • The overall architecture of Tomcat execution, how requests are processed step by step;
    • Engine, Host, Context, Wrapper concepts;
    • How the Container handles requests;
    • Related design patterns used by Tomcat;

Tomcat Top-level Architecture

  • As the saying goes, when you stand on the shoulders of giants to see the world, you should first have an overview of the whole, then break down the parts one by one, and finally form ideas and understand the details. Tomcat has a complex structure, but it is very modular. Only when you find the most core module of Tomcat, can you easily solve the problems. Understanding the overall architecture of Tomcat is essential for further understanding of Tomcat!

  • First, the top-level structure of Tomcat (Figure A) is shown as follows:

  • The topmost container in Tomcat is Server, which represents the entire Server. As shown in the figure above, a Server can contain at least one Service, that is, multiple services to provide specific services.

  • A Service consists of two main parts: Connector and Container. As you can see from the figure above, these two components are at the heart of Tomcat, and their roles are as follows:

    • Connector is used to handle connection-related matters and provide Socket and Request and Response conversion.
    • Container encapsulates and manages servlets and handles Request requests.
  • A Tomcat has only one Server. A Server can contain multiple Services, and a Service has only one Container, but it can have multiple Connectors. This is because a Service can have multiple connections, for example, providing Http and Https links simultaneously. It can also provide connections to different ports of the same protocol, as shown below (Engine, Host, Context) :

  • Multiple connectors and a Container form a Service. With a Service, it can provide external services. However, a Service needs a living environment. So the entire Tomcat life cycle is controlled by the Server.

  • In addition, the above inclusion or parent-child relationship can be seen in the server. XML configuration file in the conf directory of Tomcat. The following image shows a complete server. XML configuration file with the comments removed (Tomcat version 8.0).

  • For details about the configuration file, visit Tomcat Configuration File on the Tomcat website

  • The configuration file above can also be better understood by the following structure diagram:

  • The port number set on the Server label is 8005. Shutdown = “shutdown” means that the shutdown command is listened on port 8005. If the command is received, Tomcat is shutdown. A Server has one Service, which can also be configured. A Service has multiple Connectors. The content on the left of a Service belongs to a Container.

Summary of Tomcat top-level architecture

  1. Tomcat has only one Server. A Server can have multiple services, and a Service can have multiple Connectors and a Container.

  2. Server controls the life and death of Tomcat;

  3. Service is to provide services externally.

  4. Connector is used to accept requests and encapsulate them into Request and Response for specific processing.

  5. Container encapsulates and manages servlets and handles request requests.

  • Knowing the hierarchical architecture of Tomcat as well as the relationships and functions among components, Server and Service are far away from us for most developers, and most of the configuration content in our development belongs to Connector and Container. So let’s talk about Connector and Container.

The delicate relationship between Connector and Container

  • From the above, we can roughly know that after a Request is sent to Tomcat, it will first go through Service and then be handed to our Connector. The Connector is used to receive the Request and encapsulate the received Request into Request and Response for specific processing. After the Request and Response are wrapped, they are sent to the Container for processing. After the Container processes the Request, the Container sends the Request back to the Connector. Finally, the Connector sends the processing result back to the client through the Socket.

  • At the bottom of the Connector layer, the Socket is used for connection. Request and Response are wrapped in HTTP protocol, so the Connector needs to implement both TCP/IP and HTTP protocol!

  • Tomcat needs to receive the request first. To receive the request, we first need to look at the Connector!

  • Connector architecture Analysis

  • The Connector accepts and encapsulates a Request into a Request and a Response, and then sends the Request to the Container for processing. After the Container processes the Request, the Connector returns the Request to the client.

  • Therefore, Connector can be divided into four aspects:

    • How does Connector accept requests?
    • How to encapsulate a Request into Request and Response?
    • How do encapsulated Request and Response packets be sent to the Container for processing?
    • How do I send the Container to the Connector and return it to the client?
  • First take a look at the structure diagram of Connector (Figure B), as shown below:

  • A Connector uses a ProtocolHandler to handle requests. Different protocolHandlers represent different types of connections, such as: Http11Protocol uses the normal Socket to connect, Http11NioProtocol uses the NioSocket to connect.

  • The ProtocolHandler consists of three components: Endpoint, Processor, and Adapter.

    1. The Endpoint processes the network connection of the underlying Socket. The Processor encapsulates the Socket received by the Endpoint into a Request. The Adapter sends the Request to the Container for processing.
    2. The Endpoint processes low-level Socket network connections. Therefore, the Endpoint implements THE TCP/IP protocol, while the Processor implements the HTTP protocol. The Adapter ADAPTS requests to the Servlet container for specific processing.
    3. The Endpoint abstraction implements the Acceptor and AsyncTimeout inner classes defined in AbstractEndpoint and a Handler interface. Acceptor is used to listen for requests, AsyncTimeout is used to check the timeout of asynchronous requests, and Handler is used to process the received Socket and internally invoke the Processor for processing.
  • So far, we should be able to easily answer questions 1, 2, and 3, but we still don’t know questions 4. Let’s take a look at how the Container handles the processing and how the processing results are returned to the Connector.

Container Architecture Analysis

  • Container encapsulates and manages servlets and processes Request requests. A Container contains four sub-containers as shown in Figure C.

  • The functions of the four sub-containers are as follows:

    1. Engine: an Engine used to manage multiple sites. A Service can have at most one Engine.
    2. Host: represents a site, can also be called a virtual Host, by configuring the Host can add site;
    3. Context: represents an application, corresponding to a set of programs that are normally developed, or a Web-INF directory with the following web.xml file;
    4. Wrapper: Each Wrapper encapsulates a Servlet;
  • Find a Tomcat file directory for comparison, as shown below:

  • The difference between Context and Host is that Context represents an application. In our Tomcat default configuration, each folder directory under Webapps is a Context. The ROOT directory holds the main application and the other directories hold the child applications. The entire Webapps is a Host site.

  • When you access the Context, you can use the domain name to access the Context. www.baidu.com, if it is other applications under Host (webapps), you can use www.baidu.com/docs to access, of course, the default specified ROOT application (ROOT) can be set, but the default main application under Host site is ROOT directory.

  • We know what a Container is, but we still don’t know how the Container handles the request and returns the result to the Connector. Don’t worry! Here’s how the Container works!

How does the Container handle requests

  • Container processing requests are handled using the Pipeline-Valve Pipeline! (Valve means Valve)

  • The pipeline-valve responsibility chain mode refers to that in the process of a request processing, many processors process the request in turn, and each processor is responsible for its own processing. After processing, the result of processing will be returned, and then the next processor will continue processing.

  • But! The responsibility chain model used by Pipeline-Valve is a little different from the normal responsibility chain model! There are two main differences:

    • Each Pipeline has a specific Valve, and it is the last one executed in the Pipeline. This Valve is called BaseValve, and the BaseValve cannot be deleted;

    • The pipes of the lower container are called in the BaseValve of the pipes of the upper container.

  • We know that Container contains four subcontainers, and the corresponding BaseValve of these four subcontainers is respectively in: StandardEngineValve, StandardHostValve, StandardContextValve, StandardWrapperValve.

  • The processing flow chart of Pipeline is as follows (Figure D) :

  • After receiving the request, the Connector will first invoke the top-level container Pipeline to process the request. The top-level container Pipeline here is EnginePipeline (EnginePipeline).

  • The Engine pipe calls EngineValve1, EngineValve2, and so on, and finally StandardEngineValve, which calls the Host pipe, Then Host HostValve1, HostValve2, etc., and StandardHostValve is executed, then Context pipes and Wrapper pipes are called, and StandardWrapperValve is executed.

  • When executed to StandardWrapperValve, FilterChain is created in StandardWrapperValve and its doFilter method is called to process the request, The FilterChain contains the Filter and Servlet configured to match the request. The doFilter method calls the doFilter method of all the filters and the service method of the Servlet in turn, and the request is processed!

  • When all the pipeline-Valve processes are complete and the specific request is processed, the result is returned to the Connector, which then returns the result to the client over the Socket.

conclusion

  • So far, we have had A general understanding of the overall architecture of Tomcat. From Figures A, B, C, and D, we can see the basic elements and functions of each component. We should have an outline in our mind! If you were asked to talk briefly about Tomcat during your interview, would you be able to blurt out the above? Your interviewer will be impressed when you can just blurt it out!