An overview,

A simple introduction

  • Tomcat is a free, open source Web application server that is a lightweight application server.
  • It is the first choice for developing and debugging JSP programs when it is widely used in small and medium-sized systems and where there are not many concurrent users.
  • A core project of the Apache Software Foundation Jakarta Project, developed by Apache, Sun, and a number of companies and individuals. It is loved by Java hobbyists and recognized by some software developers. Compared with Apache or Nginx, Tomcat, a popular Web application server, has the function of processing HTML pages. However, its ability to process static HTML is far less than that of Apache or Nginx. So Tomcat is typically run as a Servlet and JSP container on the back end alone.

Core components

  • The Web container —— performs the functions of the Web server.
  • The Servlet container —— is named Catalina and is used to handle Servlet code.
  • The JSP container —— is used to translate JSP dynamic Web pages into Servlet code.


A JavaServlet is a program that runs on a Web server or application server and acts as an intermediary between a request from a Web browser or other HTTP client and a database or application on the HTTP server. Using servlets, you can collect user input from Web forms, render records from databases or other sources, and create Web pages on the fly. Similar to CGI (Common Gateway Interface) functionality.

The JSP container

  • JSP full name: Java Server Pages
  • Is a dynamic web development technology. It uses JSP tags to insert Java code into HTML Web pages. Tags usually start with <% and end with %>. JSP is a Java servlet, mainly used to implement the user interface part of Java Web application.
  • JSP acquires user input data through Web forms, accesses databases and other data sources, and then dynamically creates Web pages.

Tomcat top-level architecture

  • The top-level container in Tomcat is the Server, which represents the entire Server. A Server can contain at least one Service, which is used to provide specific services.
  • The Service consists of two main components: the Connector and the Container. The heart of Tomcat is these two components, which are used for: Connector is used to handle connection-related matters and provide Socket conversions related to Request and Response. Containers are used to encapsulate and manage servlets and specifically handle requests.
  • A Tomcat can have only one Server, a Server can contain multiple services, a Service can have only one Container, but there can be multiple Connectors, because a Service can have multiple connections, such as providing both HTTP and HTTPS links. Connections to different ports of the same protocol can also be provided.
  • Multiple connectors and a Container form a Service. With a Service, it can provide services to the outside world. However, the Service also needs a living environment. So the entire Tomcat lifecycle is controlled by the Server. In addition, the inclusion or parent-child relationship described above can be seen in the Server.xml configuration file in the Tomcat conf directory.
  • This will cause Tomcat6 to start a server instance (i.e. a JVM) that listens on port 8005 for the “shutdown” command. If it does, Tomcat is shut down. Server definitions cannot use the same port, which means that if multiple Server instances are started on the same physical machine, they must be configured to use different ports.

The role of child containers

  • Engine: Engine for managing multiple sites. A Service can have at most one Engine.
  • Host: Represents a site, also known as a virtual Host, which can be configured to add sites.
  • Context: represents an application, which corresponds to a set of programs that are normally developed, or a WEB-INF directory and the following web.xml file:
  • Wrapper: Each -wrapper encapsulates a Servlet.

The Tomcat request process

  • The user enters the URL in a browser, and the request is sent to the native port 8080, where it is picked up by the Coyote HTTP/1.1 Connector listening.
  • The Connector passes the request to the Engine (Container) of its Service to process it and waits for the Engine to respond.
  • Engine gets request localhost/test/ index.jsp to match all virtual hosts.
  • The Engine matches to a Host named localhost (if it does not match, it passes the request to that Host because this Host is defined as the default Host for the Engine), The Host named localhost gets the request /test/index.jsp to match all the contexts it owns. The Host matches to the Context with the path /test (if not, pass the request to the Context with the path name “”).
  • Path -” /test” Context gets the request/index.jsp and finds the corresponding servlet. Context in its mapping table that matches the Servlet with the URL Pattern *.jsp. Corresponding to the JSPServlet class.
  • Construction of HttpServletRequest object and HttpServletResponse object, as parameters to call the JSPServlet doGet () or doPost(), execute business logic, data storage, etc.
  • The Context returns the HttpServletResponse object to the Host after execution.
  • The Host returns the HttpServletResponse object to the Engine.
  • Engine returns the HttpServletResponse object to the Connector.
  • The Connector returns the HttpServletResponse object to the client Browser.

Tomcat service deployment

Install the JDK

You must install the JDK before you deploy Tomcat, because the JDK is a necessary environment for Tomcat to run.

(1) Close the firewall and transfer the software package needed to install Tomcat to /opt directory

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

(2) Install the JDK

cd /opt
rpm -qpl jdk-8u201-linux-x64.rpm 
rpm -ivh jdk-8u201-linux-x64.rpm 
java -version

(3) Set JDK environment variables

Sh export JAVA_HOME=/usr/ Java /jdk1.8.0_201-amd64 export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar export PATH=$JAVA_HOME/bin:$PATH source /etc/profile.d/ java -version

-- -- -- -- -- -- -- Java. Sh supplementary knowledge of -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the CLASSPATH: compile and run the Java program, JRE will search the specified path to the variable required class file. (class). Dt.jar: A library of runtime classes, mainly Swing packages. Jar: javac, Java, javap, javadoc, etc. JRE: Java Runtime Environment JRE: Java Runtime Environment JRE: Java Runtime Environment Java Virtuak Machine (Java Virtual Machine) enables Java programs to run class files on multiple platforms.

(4) Write a Java script to verify the installation

Vim # Write a Java program that prints "Hello World!" public class kkk { public static void main(String[] args){ System.out.println("Hello World!" ); Javac KKK. Java KKK. Java KKK

Install and start Tomcat

CD /opt/ tar ZXVF apache-tomcat-9.0.16.tar.gz mv apache-tomcat-9.0.16 /usr/local/tomcat ## / usr/local/tomcat/bin/startup. Sh netstat natp | grep 8080 # using a browser to access the tomcat home page

Optimize Tomcat startup speed

Vim/usr/Java/jdk1.8.0 _201 amd64 / jre/lib/security/Java security # modified 117 lines; /dev/urandom.source =file: /dev/urandom.source =file: /dev/urandom.source =file: /dev/urandom.source =file: /dev/urandom.source =file: /dev/urandom.source =file: /dev/urandom.source =file: /dev/urandom.source =file:/dev/urandom /usr/local/bin ln s /usr/local/tomcat/bin/* /usr/local/bin/ # shutdown

Tomcat’s main directory

Tomcat virtual host configuration

Many times a company will have multiple projects running, so it is not possible to run multiple Tomcat services on a single server, which would consume too many system resources. At this point, you need to use the Tomcat virtual host. For example, two new domain names and “are added now. We hope to access different project contents through these two domain names.

(1) Create LEI and DIAN project directories and files

mkdir /usr/local/tomcat/webapps/lei mkdir /usr/local/tomcat/webapps/dian echo 'this is lei ! ' > /usr/local/tomcat/webapps/lei/index.jsp echo 'this is dian! ' > /usr/local/tomcat/webapps/dian/index.jsp

(2) Modify the Tomcat master configuration file

Vim/usr/local/tomcat/conf/server. The XML line # 165 < Host name = "" appBase = "webapps" unpackWARs = "true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/lei" path="" reloadable="true" /> </Host> <Host name="" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/dian" path="" Reloadable ="true" /> </Host> # Restart service; Command path optimization can be used directly after shutdown. Sh and startup sh/usr/local/tomcat/bin/shutdown. Sh/usr/local/tomcat/bin/startup. Sh # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- parameters explanation -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the Host name: hostname appBase: Tomcat application working directory, relative paths for webapps, the absolute path to/usr/local/Tomcat/webapps unpackWARs: whether to unzip the war file autoDeploy: indicates a Tomcat runtime, XMLNamespaceAware: Whether XML namespaces are enabled, set this value to true in conjunction with XMLValidation. DocBase: web application directory path: sets the accessed URI to the root directory of the web application reloadable: whether to reload when the program has changed

(3) Verification

Echo " host ">> /etc/hosts

Tomcat optimization

The default configuration of the default Tomcat installation is not suitable for production environments. It may have suspended animation frequently and needs to be restarted. Only through constant test optimization can it run efficiently and stably. The optimization mainly includes three aspects: operating system optimization (kernel parameter optimization), Tomcat configuration file parameter optimization, and Java virtual machine (JVM) optimization. Today we’ll look at Tomcat configuration file parameter tuning.

Configuration file parameter optimization

Modifying configuration files

Vim/usr/local/tomcat/conf/server XML < Connector port = "8080" protocol = "HTTP / 1.1" connectionTimeout = "20000" EnableLookups ="false" DisableUploadTimeout ="true" AcceptCount ="300" maxThreads="500" processorCache="500" URIEncoding="UTF-8" compression="on" compressionMinSize="2048" compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg,image/png" /> # optimization directly with shutdown. Sh and startup. Sh/usr/local/tomcat/bin/shutdown. Sh/usr/local/tomcat/bin/startup. Sh

The last

Autumn recruit immediately, prepared a big wave of test questions for everyone! Welcome to pay attention to the public number: the future has light, receive a front-line factory JAVA interview question summary + each knowledge point learning thinking guide + a 300-page PDF document JAVA core knowledge point summary! The contents of these materials are the knowledge points that the interviewer must ask during the interview. The chapter includes many knowledge points. These include the basics, Java collections, the JVM, multithreading concurrency, Spring principles, microservices, Netty and RPC, Kafka, diaries, design patterns, Java algorithms, databases, ZooKeeper, distributed caching, data structures, and more.