preface
An overview,
A simple introduction
Tomcat is a lightweight Web application server that is free and open source code.
Being widely used in small and medium sized systems and where there are not many concurrent access users is the preferred way to develop and debug 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. 2021 Java Real question interview document to share with you.
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.
Second, Tomcat top-level architecture
(1) The top-level container in Tomcat is Server, which represents the whole Server. A Server can contain at least – – services for providing specific services.
(2) A Service consists of two main parts: Connector and Container. The heart of Tomcat is these two components. 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.
(3) 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, because a Service can have multiple connections. If both Http and Https links are provided, connections to different ports of the same protocol can also be provided.
(4) Multiple connectors and a Container form a Service. With a Service, it can provide services to the outside world. 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 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.
Function of 4 child containers
(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 normally developed, or a WEB-INF directory and the following web.xml file:
(4) Wrapper: Each -wrapper encapsulates a Servlet.
3. Tomcat request process
(1) The user types the url into a browser, and the request is sent to the native port 8080, picked up by the Coyote HTTP/1.1 Connector listening there.
(2) The Connector sends the request to the Service’s Engine (Container) for processing and waits for the Engine to respond.
(3) Engine gets request localhost/test/ index.jsp and matches all virtual Host hosts.
(4) The Engine matches a Host named localhost (if not, the request is sent to this Host because it is defined as the default Host for the Engine). 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 “”).
(5) Path -” /test” Context gets request/index.jsp, search its mapping table for servlet. Context matching the Servlet whose URL Pattern is *.jsp. Corresponds to the JspServlet class.
(6) Construct HttpServletRequest object and HttpServletResponse object, as a parameter to call doGet () or doPost() of JspServlet, perform business logic, data storage, etc.
Context returns the HttpServletResponse object to Host after execution.
(8) Host returns the HttpServletResponse object to Engine.
(9) Engine returns the HttpServletResponse object back to Connector.
(10) Connector returns the HttpServletResponse object to the client Browser.
Iv. 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
Five, 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/hosts
6. 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
I here organized a Tomcat information document Java systematic information (including Java core knowledge, Spring series of family drum, interview topics and 21 years of the latest Internet real questions, e-books, etc.) friends who need to pay attention to the public number [procedures Yuan small wan] can be obtained.