An overview,
A simple introduction
-
Tomcat is a lightweight Web application server that is free and open source code.
-
It is commonly used in small and medium sized systems and where there are not many concurrent access users
Is the first choice for developing and debugging JSP programs.
- The Apache Software Foundation’s Jakarta project is a core project developed by Apache, Sun, and a number of companies and individuals. Deeply Java enthusiasts love, and some software developers recognized. At present, Tomcat, a popular Web application server, has the function of processing HTML pages compared with Apache or Nginx Web servers, but its ability to process static HTML is far less than Apache or Nginx. So Tomcat typically runs on the back end as a Servlet and JSP container.
Core components
- Web container —— Provides the functions of the Web server.
- The Servlet container ——, named Catalina, is used to process Servlet code.
- The JSP container —— is used to translate JSP dynamic web pages into Servlet code.
JavaServlet
A JavaServlet is a program running on a Web server or application server that acts as an intermediate layer between requests from a Web browser or other HTTP client and a database or application on an HTTP server. With 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: 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 that is primarily used to implement the user interface portion of a Java Web application.
- JSP uses web forms to get user input data, access databases and other data sources, and create web pages on the fly.
Tomcat Top-level Architecture
-
The topmost container in Tomcat is Server, which represents the entire Server. A Server can contain at least one Service to provide specific services.
-
A Service consists of two main parts: Connector and Container. These two components are at the heart of Tomcat. Connector is used to handle connection-related matters and provide Socket and Request and Response related transformations. 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 can have multiple Connectors, because a Service can have multiple connections, such as providing Http and Https links at the same time. 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 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 relationship, or parent-child relationship, can be seen in the server.xml configuration file in the Conf directory of Tomcat.
-
< server port-” 8005 “shutdown-” shutdown “> this will cause Tomcat6 to start – a server instance (i.e. – a JVM) that listens on port 8005 to receive the” shutdown “command, If received, Tomcat is shut down. Each Server
The definition 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: An Engine used to manage multiple sites. A Service can have at most one Engine.
- Host: represents a site, which can also be called a virtual Host. You can add a site by configuring Host.
- Context: represents an application, corresponding 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.
Tomcat Request Process
-
The user types the url into a browser, and the request is sent to native port 8080, picked up by the Coyote HTTP/1.1 Connector listening there.
-
The Connector passes the request to the Service’s Engine (Container) for processing and waits for the Engine to respond.
-
Engine gets the request localhost/test/ index.jsp, matching all virtual Host hosts.
-
The Engine matches a Host named localhost (if not, the request is processed to that Host because it is defined as the Engine’s default Host), The Host named localhost gets the request /test/index.jsp and matches all the contexts it owns. Host matches the Context whose path is /test (if not, pass the request to the Context whose path name is “”).
-
The path-” /test” Context gets the request/index.jsp and searches its mapping table for the corresponding servlet. Context to match the URL
A Servlet whose Pattern is *.jsp corresponds to the JspServlet class.
-
Construct an HttpServletRequest object and an HttpServletResponse object that calls doGet () or doPost() of the JspServlet as arguments to perform business logic, data storage, and so on.
-
Context returns the HttpServletResponse object to Host after execution.
-
Host returns the HttpServletResponse object to Engine.
-
The 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 have the JDK installed before deploying Tomcat, because the JDK is the necessary environment for Tomcat to run.
(1) Disable the firewall and upload the software package required for Tomcat installation to the /opt directory
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
Copy the code
(2) Install JDK
cd /opt
rpm -qpl jdk-8u201-linux-x64.rpm
rpm -ivh jdk-8u201-linux-x64.rpm
java -version
Copy the code
(3) Set JDK environment variables
Vim /etc/profile.d/java.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.sh java -versionCopy the code
-- -- -- -- -- -- -- 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 environments, mainly swing packages. Tools. jar: a library of JDK tools, including Javac, Java, Javap, javadoc, etc. JDK: Java Development Kit (Java) JRE: Java Runtime Environment (Java Runtime Environment) Java Virtuak Machine (Java Virtual machine) enables Java programs to run class files on a variety of platforms.Copy the code
(4) Write a Java script to verify the installation
Vim kkK.java # Write a Java program that prints "Hello world!" public class kkk { public static void main(String[] args){ System.out.println("Hello World!" ); }} # check whether the JDK environment is set successfully javac kkK. Java Java KKKCopy the code
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 # http://192.168.19.11:8080 using a browser to access the tomcat home pageCopy the code
Optimize Tomcat startup speed
Vim/usr/Java/jdk1.8.0 _201 amd64 / jre/lib/security/Java security # modified 117 lines; Source =file:/dev/urandom # create a soft connection Run the /usr/local/bin ln -s /usr/local/tomcat/bin/* /usr/local/bin/ # command to restart tomcat shutdown.sh startup.shCopy the code
The main directory of Tomcat
Tomcat virtual host configuration
In many cases, companies will have multiple projects to run, so it is definitely not possible to run multiple Tomcat services on one server, which will consume too much system resources. At this point, you need to use Tomcat virtual host. For example, two new domain names www.lei.com and www.dian.com are added 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.jspCopy the code
(2) Modify the Tomcat master configuration file
Vim/usr/local/tomcat/conf/server. The XML line # 165 < Host name = "www.lei.com" appBase = "webapps" unpackWARs = "true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/lei" path="" reloadable="true" /> </Host> <Host name="www.dian.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/dian" path="" Reloadable ="true" /> </Host> 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, If you have a new WEB application that allows automatic deployment of xmlValidation, set this value with xmlValidation to true. DocBase: indicates the directory of the Web application. Path: Indicates that the URI for accessing the web application is set to the root directory of the Web application. Reloadable: indicates whether the program is reloaded when changes are madeCopy the code
(3) verification
Echo "192.168.19.11 www.lei.com www.dian.com" >> /etc/hostsCopy the code
Tomcat optimization
The default configuration of Tomcat under the default installation is not suitable for the production environment. It may be frequently suspended and needs to be restarted. Only through constant pressure test and optimization can it run efficiently and stably. Optimization mainly includes three aspects: operating system optimization (kernel parameter optimization), Tomcat configuration file parameter optimization, and Java Virtual Machine (JVM) tuning. Today we will take a look at optimizing Tomcat configuration file parameters.
Optimized configuration file parameters
Modifying a Configuration File
Vim/usr/local/tomcat/conf/server XML < Connector port = "8080" protocol = "HTTP / 1.1" connectionTimeout = "20000" RedirectPort ="8443" #72 line insert minSpareThreads="50" 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. ShCopy the code
The last
Immediately autumn recruit, for everyone to prepare a big wave of questions! Welcome to pay attention to the public number: the future has light, receive a line of large factory Java interview questions summary + the knowledge points learning thinking guide + a 300 page PDF document Java core knowledge points summary! These are some of the things that the interviewer should ask during the interview. These include basics, Java collections, JVMS, multi-threaded concurrency, Spring principles, microservices, Netty and RPC, Kafka, diaries, design patterns, Java algorithms, databases, Zookeeper, distributed caching, data structures, and more.