Getting started with Tomcat


Tomcat is a term familiar to Internet practitioners. It is a well-known HTTP server based on Java. The Java programmer development website is actually a secondary development based on Tomcat.

insert image description here

1. Download and install

First of all, the premise of running Tomcat is to install the JDK. I think everyone has started to learn Tomcat. IDEA is naturally installed. After IDEA is installed, the JDK must also be installed. I will not go into details about the installation of JDK here.

Tomcat 8 is installed here

Official website address

insert image description here

After downloading, you will get a compressed file, you only need to decompress the zip file, and the download is complete (the decompressed file is best placed in the English directory)

insert image description here

In the unzipped folder, you need to focus on the

  • bin: This directory contains all kinds of 启动, 停止程序
    * logs: The log files of the Tomcat runtime are placed, which can reflect the running status of the program and will be checked frequently by us to find problems.
  • webapps: Stores the webapp that needs to be run (a fully functional website is called webapp), and then deploys it, that is, put the packaged files into the webapps directory, which is the most commonly used folder

2. Start Tomcat

Since Tomcat is a cross-platform server, it can be downloaded on both Windows and Linux

On Windows, double-click startup.batto start the server

On Linux, double-click startup.shto start the server

insert image description here

On Windows, it will be started by cmd, and we will see the following log, which means the startup is successful

insert image description here

Since Tomcat handles Chinese according to the UTF-8 encoding by default, and the Windows cmd defaults to the GBK encoding, garbled characters will appear, but it has little effect.

Some friends may experience a flashback. If you want to see the specific reason for the startup failure, you only need to drag the startup program startup.bat to cmd to run, and you can display the reason for the error, perhaps because of environment variables. , maybe because of the JDK, or maybe because of other reasons

3. Access Tomcat

After starting Tomcat, you only need to enter 127.0.0.1:8080(the host's own loopback IP, 8080 is the default port number of Tomcat) in the browser to see the default welcome page of Tomcat returned by the browser

insert image description here

Fourth, deploy static pages

A static page is a page with the same content, such as an HTML file written

Next, you can write a simple HTML file, deploy it to Tomcat, and then access it through Tomcat

  1. Simple HTML file
<!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>Document</title>
</head>
<body>
    Hello world!
</body>
</html>

2. Put the file in the ROOT directory in webapps

insert image description here

  1. Start Tomcat and enter 127.0.0.1:8080/helo.html in the browser

insert image description here

In this way, you can access the code you wrote.

This is just a simple HTML file. If there are other dependent resources in this file, such as a lot of CSS, JS, pictures and other files, if you want Tomcat to access, you must deploy these resources together in the webapps directory

  1. HTML files and their associated resources

HTML file

<!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>Tomcat</title>
    <link rel="stylesheet" href="CSS/css1.css">
</head>
<body>
    <div>
        <img src="image/picture.jpg" alt="">
    </div>
    <script src="JS/js1.js"></script>
</body>
</html>

CSS file

div {
    
    
    height: 200px;
    width: 200px;
    background-color: red;
}
div img {
    
    
    height: 150px;
    width: 150px;
    margin: 25px;
}

js file

alert("Tomcat!");
  1. Create a folder (named Tomcat here), which contains HTML files and various resources that need to be accessed. Various resources are put into their respective types of folders for easy sorting. The Tomcat folder that will be created will be created. Put it in the webapps directory, at the same level as the ROOT folder

insert image description here

  1. Start Tomcat and enter 127.0.0.1:8080/Tomcat/tomcat.html in the browser

Here Tomcat is the first-level directory under webapps, and tomcat.html is the HTML file we want to access. Be sure to check each level of directory, otherwise the resource will not be found, resulting in 404

insert image description here

page effect

insert image description here
Finish!

Guess you like

Origin blog.csdn.net/weixin_46103589/article/details/123523571