1. Introduction to Tomcat Services
Tomcat server is a free, open source Web application server. It is a lightweight application server and is widely used in small and medium-sized systems and in situations where concurrent access to users is not very common. It is the preferred choice for developing and debugging JSP programs.Generally speaking, although Tomcat, like Web servers such as Apache or Nginx, has the ability to process HTML pages, Tomcat is usually run as a Servlet and JSP container on its own in the back-end because it is far less capable of handling static HTML than Apache or Nginx.Figure:
Tomcat's performance has been greatly improved since version 5.x, along with its open framework and redevelopment features, it is now fully usable in production environments with limited access.Tomcat is currently used by most e-commerce websites for JSP technology development.
2. Install Tomcat Service
JDK must be installed before Tomcat can be installed.The full name of JDK is Java Development Kit, which is a free software development kit for the Java language provided by Sun Company, including the Java Virtual Machine (JVM).Written Java source programs are compiled to form Java byte codes. Once JDK is installed, these byte code files can be interpreted using JVM, which ensures the cross-platform nature of Java.
In terms of platform compatibility, there are different versions of JDK as a Java virtual machine that interprets the byte code file and invokes the operating system API to implement the corresponding function, which is closely related to the operating system type and the number of bits on the platform, and Tomcat also has the above characteristics.Centos 7 already has a JDK installed by default.So just download the Tomcat software.
The package apache-tomcat-8.5.16.tar.gz network link used in this case: https://pan.baidu.com/s/11Puem2_pMMhEnWAgriJ-sA
Extraction Code: 3mzo
(1) Check the Java environment
[root@localhost ~]# java -version openjdk version "1.8.0_102" OpenJDK Runtime Environment (build 1.8.0_102-b14) OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode) //Viewed by command, the installation is complete.
(2) Install and configure Tomcat
The installation of the Tomcat service is simple, as follows:
[root@localhost ~]# tar zxf apache-tomcat-8.5.16.tar.gz -C /usr/src [root@localhost ~]# mv /usr/src/apache-tomcat-8.5.16/ /usr/local/tomcat8 //Because the Tomcat software is very simple, there is no need to install it //For future use, we recommend replacing the unzipped directory and renaming it [root@localhost ~]# /usr/local/tomcat8/bin/startup.sh //Start the Tomcat service using the startup script that comes with the Tomcat program Using CATALINA_BASE: /usr/local/tomcat8 Using CATALINA_HOME: /usr/local/tomcat8 Using CATALINA_TMPDIR: /usr/local/tomcat8/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar Tomcat started. [root@localhost ~]# netstat -anpt | grep 8080 tcp6 0 0 :::8080 :::* LISTEN 3493/java //View its listening port (Tomcat service defaults to 8080) [root@localhost ~]# firewall-cmd --add-port=8080/tcp success //Open the port of a firewall's services
(3) Client access
3. Tomcat Profile Details
(1) Instructions for Tomcat configuration
The home directory where you just installed the move is/usr/local/tomcat8/
[root@localhost ~]# ll /usr/local/tomcat8/ //Total usage 92 drwxr-x---. 2 root root 4096 9 April 23:56 bin drwx------. 3 root root 254 9 April 23:57 conf drwxr-x---. 2 root root 4096 9 April 23:56 lib -rw-r-----. 1 root root 57092 6 February 22, 2017 LICENSE drwxr-x---. 2 root root 240 9 Month 500:02 logs -rw-r-----. 1 root root 1723 6 February 22, 2017 NOTICE -rw-r-----. 1 root root 7064 6 February 22, 2017 RELEASE-NOTES -rw-r-----. 1 root root 15946 6 February 22, 2017 RUNNING.txt drwxr-x---. 2 root root 30 9 April 23:56 temp drwxr-x---. 7 root root 81 6 February 22, 2017 webapps drwxr-x---. 3 root root 22 9 April 23:57 work
Main Directory Description:
- bin: Store script files that start or close Tomcat on Windows or Linux platforms;
- conf: Stores various global configuration files for the Tomcat server, the most important of which are server.xml and web.xml;
- lib: Store the library files (JARS) that Tomcat needs to run;
- logs: Store the LOG file when Tomcat executes;
- webapps: Tomcat's main Web publishing directory (including application instances);
- work: Stores the class file generated by the JSP compilation.
[root@localhost ~]# ll /usr/local/tomcat8/conf/ //Total usage 224 drwxr-x---. 3 root root 23 9 April 23:57 Catalina -rw-------. 1 root root 13816 6 February 22, 2017 catalina.policy -rw-------. 1 root root 7376 6 February 22, 2017 catalina.properties -rw-------. 1 root root 1338 6 February 22, 2017 context.xml -rw-------. 1 root root 1149 6 February 22, 2017 jaspic-providers.xml -rw-------. 1 root root 2358 6 February 22, 2017 jaspic-providers.xsd -rw-------. 1 root root 3622 6 February 22, 2017 logging.properties -rw-------. 1 root root 7511 6 February 22, 2017 server.xml -rw-------. 1 root root 2164 6 February 22, 2017 tomcat-users.xml -rw-------. 1 root root 2633 6 February 22, 2017 tomcat-users.xsd -rw-------. 1 root root 168251 6 February 22, 2017 web.xml
Main profile description:
- catalina.policy: Rights control profile;
- catalina.properties:Tomcat property profile;
- context.xml: Context configuration file;
- logging.properties: log log-related configuration files;
- server.xml: main configuration file;
- tomcat-users.xml:manager-gui management configuration file (Tomcat provides a manager management interface by default after installation, which can be opened for access by configuring it);
- web.xml: Tomcat's servlet, servlet-mapping, filter, MIME, and other related configurations.
(2) Tomcat master profile description
server.xml is Tomcat's main configuration file. By configuring this file, you can modify Tomcat's startup port, website directory, virtual host, https and other important functions.
The whole server.xml consists of the following structures: <Server>, <Service>, <Connector /><Engine >, <Host>, <Context>, </Context></Host></Engine></Service>, and </Server>.
The following are some of the contents of the default installation server.xml file, where <!----> contains annotation information (annotation information is already attached to frequently modified places).
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml <?xml version="1.0" encoding="UTF-8"?> ............ //Omit some content <Server port="8005" shutdown="SHUTDOWN"> //Tomcat closes the port, which is only open to local addresses by default and is accessible locally through Telnet 127.0.0.1 8005. //Close Tomcat ............ //Omit some content <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> //The default port number for Tomcat startup is 8080, which can be changed as needed. ............ //Omit some content <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> //The default port number when Tomcat starts the AJP 1.3 connector and can be changed as needed ............ //Omit some content //Below are the configurations and log configurations for Tomcat when defining virtual hosts <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server>
(3) Description of the components of Tomcat Server
The server element represents the servlet container for the entire CatAlina.
A Service is a collection of one or more Connectors and an Engine that handles all client requests made by the Connector.
A Connector listens to a customer request on a specified port and handles the request to Engine, gets a response from Engine, and returns it to the customer.
Tomcat has two typical connectors: one that listens directly for http requests from browser and one that listens for requests from other webserver s.
- Coyote http/1.1 Connector listens for http requests from client browser at port 8080;
- Coyote JK2 Connector listens for servlet/jsp proxy requests from other text server s (Apache) at port 8009.
You can configure multiple virtual host s under Engine, each of which has a domain name.
When Engine receives a request, it matches it to a Host and handles the request to that host.
Engine has a default virtual host that will be handled when a request cannot be matched to any of the hosts.
Host represents a virtual Host, each of which matches a domain name Domain Name.
One or more web apps can be deployed under each virtual host, with each web app corresponding to a Context and a Context path.
When the host receives a request, it will match the request to a Context, which will then be handled by the Context by Maximum Match, so a Context with path="" will become the default Context for the Host.
All requests that cannot match the path name of another Context will eventually match the default Context.
A Context corresponds to a web application, and a web application consists of one or more servlet s.
4. Establishing a virtual directory site for Java
(1) Create a virtual directory for storing Web site files
[root@localhost ~]# mkdir -p /web/webapp
(2) Create a test file written in Java language
[root@localhost ~]# vim /web/webapp/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("welcome to test");%> </body> </html>
(3) Modify Tomcat's main configuration file
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml ........................ //Omit some content <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> //Host defines a virtual host with a domain name of localhost, appBase defines the application base directory, unpackWARs defines whether to decompress automatically, and autoDeploy defines whether to deploy automatically <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <Context docBase="/web/webapp" path="" reloadable="false"> </Context> //context defines a web application (virtual directory), path specifies an access directory, docBase defines a web directory, automatically loads when the application changes without restarting tomcat (add <context manually...></context>Content) </Host> [root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh [root@localhost ~]# /usr/local/tomcat8/bin/startup.sh //Restart Tomcat Service
(4) Client Access Test
This indicates that the Tomcat service virtual directory is in effect!