[Tomcat] Tomcat introduction and tutorial

1. Introduction to Tomcat

  • Apache Tomcat is an open source developed by the Apache Software Foundation (ASF)Java Web Application Server

  • Since Tomcat is implemented by the Java language, it needs to run on the Java virtual machine, soJDK must be installed before use, to provide the runtime environment

  • Tomcat implements several Java EE specifications, including Java Servlet, Java Server Pages (JSP), Java Expression Language, and Java WebSocket, among others. After downloading Tomcat, you can see the reference to the relevant Java EE specification API source code in its lib directory

    image-20220307085615926

  • Among the several Java EE specifications implemented by Tomcat, there is a very important specificationServlet, through which we can run our own servlet applications to handle dynamic requests, that isImplement dynamic pages

  • Tomcat's Connector componentImplemented parsing of HTTP requests,CanThink of Tomcat as an HTTP server, Tomcat can receive and parse HTTP requests through the Connector component, and then pass the parsed information to Servlet for processing:

    • For static resource (html/css/js, etc.) requests: Tomcat provides a default servlet to handle the response
    • For dynamic requests: it can be mapped to a servlet application written by yourself to handle

2. Download and install

Here we take Tomcat 8 as an example to demonstrate how to download and install Tomcat in Windows and Linux systems respectively.

Note: Since Tomcat runs on a Java virtual machine, you must download the JDK before installing Tomcat

2.1 Installation in Windows

  • Enter the Tomcat official website: https://tomcat.apache.org/

  • Click Tomcat 8 in the download bar

image-20220307092251057

  • Select the zip archive, download it and unzip it

    image-20220307092503087

  • Open the Tomcat folder, enter the bindirectory , and double-click startup.batto run the Tomcat server

    image-20220307104707460

  • Note: Since the default encoding method of CMD is GBK, and the default encoding method of Tomcat is UTF-8, when you double-click startup.batto start Tomcat, garbled characters will be displayed on CMD. But this problem can not be dealt with, because it is not necessary to check the startup status of Tomcat and other situations through CMD, and other methods can be used.

    image-20220307195837075

2.2 Installation in Linux

  • First switch to the opt directory (opt is the directory where additional software (installation packages) are stored for the host)

    cd /opt
    
  • Download the Tomcat installation package through wget

    wget https://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.5.73/bin/apache-tomcat-8.5.73.tar.gz
    
  • Unzip the downloaded Tomcat archive

    tar xzf apache-tomcat-8.5.73.tar.gz
    
  • After that startup.shrun Tomcat server by

    /opt/apache-tomcat-8.5.73/bin/startup.sh
    

2.3 Access Tomcat

After we run Tomcat, we 服务器IP端口号can .

  • If you are using a server, then the IP address is the public IP of the server
  • If you are using your own host, the IP address is127.0.0.1
  • The default port number of Tomcat is 8080(can be manually modified in the conf directory)

After successful access, the following page will be displayed:

image-20220307150849569

Notice: If you want to access other people's Tomcat, either the other person's Tomcat uses the external network IP, or you and his external network must be the same, that is, under the same local area network. Only in these two cases can you access other people's Tomcat, otherwise you will not be able to access

Currently, the IP protocol we use is IPv4, and the IP address under this protocol is a 32-bit, 4-byte integer. Since this number is not large, but the number of networked devices in the world has exceeded this number, so we want every It is not enough for every host to have a single IP address.

Therefore, the NAT mechanism is adopted, that is, multiple hosts use one external network IP, the external network is different and repeated, and each different host under the same external network has its own internal network IP. In this way, the problem of insufficient IP addresses is well solved.

3. Tomcat's directory structure

When Tomcat is installed, open its folder and you can see the following directories

image-20220307151613184

  • bin: scripts that store various startup, shutdown and other programs ( *.shfiles are used for Unix systems, *.batfiles are used for Windows systems, and *the functions of the same programs are the same)

  • conf: The directory where configuration files and related data files are stored, such as storage server.xml, tomcat-users.xml,web.xml

  • lib: The directory where the library files used by Tomcat are stored, such as the API that stores the Servlet specification

  • logs: The directory where the default log files are stored, such as access logs (you can configure the logs to other directories through the server.xmlfile )

  • temp: The working directory of temporary files, such as cache data when uploading large files will be stored here

  • webapps: The directory where web applications are stored and used for program deployment (can be configured through server.xmlfiles )

    A website with independent and complete functions can be called a web application. Multiple such web applications can be deployed on a Tomcat server at the same time. These web applications are stored as directories in the webapps directory

  • work: Tomcat's working directory, such as storing JSP compiled class files

4. Tomcat configuration file

In the conf directory of Tomcat, the configuration files of Tomcat are stored. Open the directory and you can see that there are mainly the following configuration files

image-20220307202456120

  • catalina.policy: This configuration file is read when a Tomcat instance is started based on the -securtyoption . This file is a Java security policy configuration file that configures permissions to access the codebase or certain Java classes
  • catalina.properties: Java property definition file, setting class loader path, security package list and some parameter information for tuning performance
  • context.xml: The default configuration file provided for deploying web applications on this Tomcat instance, each webapp can use unique context.xml, usually placed in the META-INF subdirectory of the webapp directory, often used to define session managers , Realm and JDBC etc.
  • logging.properties: Define log related configuration information, such as log level, file path, etc.
  • server.xml: Tomcat core configuration file, including the configuration information of the main components of Service, Connector, Engine, Realm, Valve, and Hosts
  • tomcat-users.xml: Contains the relevant roles, users and passwords used in Realm authentication; the manager that comes with Tomcat will use this file by default; add and delete users in Tomcat, assign roles to users, etc. Do this by editing this file
  • web.xml: Provides deployment descriptors for all web applications deployed on Tomcat instances, typically used to provide default servlet definitions and basic MUIME mapping tables for webapps

5. WEB application deployment directory structure

When deploying an application, it is usually packaged into a warpackage and placed in Tomcat's application deployment directory webapps. The web application has a specific organizational format, which is a hierarchical directory structure, usually including servlet code files, HTML, JSP page files, class files, deployment descriptor files, etc. The relevant instructions are as follows:

  • /: Represents the root directory of the web application, which can hold HTML, JSP pages, and other files (such as JS, CSS, image files) that must be visible to the client browser. In larger applications, there is also an option to divide these files into subdirectory hierarchies

  • /WEB-INF: Indicates all the private resource directories of the web application, which cannot be accessed by the user's browser. Usually web.xmland context.xmlare placed in this directory.

  • /WEB-INF/web.xml: Represents a private deployment descriptor for a web application, describing the servlets and other components (such as filters) that make up the application, as well as related initialization parameters and container-managed security constraints.

  • /WEB-INF/classes: Indicates the storage directory of the Java program class files and related resources of the web application.

  • /WEB-INF/lib: Represents the web application's own JAR file, which contains Java class files and related resources (such as third-party class libraries or JDBC drivers) required by the application

6. Deploy static pages

What is a static page?

A static page is a page whose content is always fixed. Even if the user is different, the time is different, and the input parameters are different, the content of the page will not change. Unless the developer of the website modifies the source code, the content of the page remains the same.

6.1 Deploying a single HTML

You can deploy a single HTML you wrote to Tomcat as follows:

  • Create an html file first ( hello.html)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>将 HTML 部署到 Tomcat 上</title>
    </head>
    <body>
        <div>hello</div>
    </body>
    </html>
    
  • Copy the created html file to the Tomcat webapps/Rootdirectory

    image-20220308002829005

  • Start Tomcat and enter in the URL bar of the browser 127.0.0.1:8080/hello.htmlto access the HTML just deployed

    image-20220308003023378

6.2 Deploy HTML with CSS, JavaScript, and Images

In actual development, HTML may not only be a single file, but also depend on some other resources, such as CSS, JavaScript, images, and so on.

You can deploy your own HTML with CSS, JavaScript or images to Tomcat as follows:

  • Create a html file ( hello.html)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>将 HTML 部署到 Tomcat 上</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <img src="弥豆子.jpg" alt="弥豆子">
        <script src="app.js"></script>
    </body>
    </html>
    
  • Create a css file ( style.css)

    img {
          
          
        width: 500px;
        height: 300px;
    }
    
  • Create a js file ( app.js)

    console.log("hello");
    
  • ready picture ( 弥豆子.jpg)

  • Copy the above files to the Tomcat webapps/ROOTdirectory

    image-20220308004456692

  • Start the server and enter in the URL bar of the browser 127.0.0.1:8080/hello.htmlto access the HTML just deployed

    image-20220308004556839

6.3 Deploying HTML into a separate directory

In actual development, there may be more than one HTML file, and there are many dependent resources, which are very messy. Therefore, it is not suitable to copy all directly into the webapps/ROOTdirectory .

A separate directory can be created, side by side with the ROOT directory, to store the content we want to deploy to Tomcat, as follows:

  • Create a new folder (here HelloApp) in the webapps directory, and in this file, create a new directory to reasonably classify the HTML dependencies (such as css, js, img, etc.)

  • Copy all the files in 6.2 to this file, and classify them according to the newly created directory (Note: It may be necessary to adjust the paths of some resources

    image-20220308005701596

  • Start Tomcat and enter in the URL bar of the browser 127.0.0.1:8080/HelloApp/hello.htmlto access the HTML just deployed

    image-20220308010153749

6.4 Deploy the blog system page

I have written a front-end page of a blog system before (the source code and comments are written in great detail. If you need it, you can read " Personal Simple Blog System Page Construction (with source code) ". Here is the page of this blog system as an example. , deploy it on Tomcat

  • First create a directory under the webapps directory (here is blog)

  • Copy the files of the blog system page written in

    image-20220308011652995

  • Start Tomcat and enter in the URL bar of the browser 127.0.0.1:8080/blog/blog_list.htmlto access the blog system just deployed

    insert image description here

6.5 Deploy the blog system to the cloud server

The above has introduced how to deploy web programs such as HTML files to the local Tomcat, but just like that, others cannot access these pages.

Therefore, you can deploy the web program to the webapps directory of Tomcat on the cloud server, so that other people can see the program you deploy. The way is as follows:

  • First, make a zip archive for the directory of the program you want to deploy (here, take the above blog system page as an example)

    image-20220308013052193

  • Connect to the cloud server through xshell and switch to the webapps directory of Tomcat

    image-20220308013613749

  • Drag and drop blog.zip into xshell (you can also use other methods, such as using xftp)

    image-20220308014036692

  • After the drag and drop is successful, unzip the blog.zip

    unzip. blog.zip
    

    image-20220308014107782

  • Finally, http://:8080/blog/blog_list.htmlyou can and everyone can also access it!
    insert image description here

Guess you like

Origin blog.csdn.net/weixin_51367845/article/details/123429050