5. Tomcat server - installation, use, deployment configuration in Idea (super detailed)

1. The concept of JavaWeb

  • 1. What is JavaWeb
    JavaWeb refers to the general term for all programs written in the Java language that can be accessed through a browser, called JavaWeb.
    JavaWeb is developed based on requests and responses.

  • 2. What is a request?
    A request means that the client sends data to the server, which is called a request.

  • 3. What is a response
    Response means that the server returns data to the client, called Response.

  • 4. The relationship between request and response
    Request and response appear in pairs, there is a request and there is a response.
    insert image description here

2. Classification of web resources

Web resources are divided into static resources and dynamic resources according to the different technologies and presentation effects.
Static resources: html, css, js, txt, mp4 videos, jpg pictures
Dynamic resources: jsp pages, Servlet programs

3. Commonly used web servers

  • Tomcat: A Web server provided by the Apache organization, providing support for jsp and Servlet. It is a lightweight javaWeb container (server) and the most widely used JavaWeb server (free).

  • Jboss: It is an open source, pure Java EJB server that complies with the JavaEE specification, and it supports all JavaEE specifications (free of charge).

  • GlassFish: A Java Web server developed by Oracle Corporation, is a robust commercial server, production-quality (very few applications).

  • Resin: It is a product of CAUCHO Company. It is a very popular server. It provides good support for servlet and JSP, and its performance is relatively
    good. Resin itself is developed in JAVA language (fees are charged, and there are many applications).

  • WebLogic: It is a product of Oracle Corporation. It is the most widely used Web server at present. It supports the JavaEE specification and is
    constantly improved to meet new development requirements. It is suitable for large-scale projects (charges, not used much, suitable for large companies).

4. Correspondence between Tomcat server and Servlet version

Currently commonly used versions 7.* and 8.*
insert image description here

Servlet program from version 2.5 is the most used version in the world (xml configuration)
to after Servlet3.0. It is used by the annotation version of Servlet.

Take version 2.5 as the main line to explain the Servlet program.

5. Using Tomcat

5.1 Installation

Find the zip archive corresponding to the Tomcat version you need to use, and unzip it to the directory to be installed.

5.2 Catalog introduction

  • bin Executable program specially used to store the Tomcat server
  • conf is specially used to store the configuration file of the Tocmat server
  • lib is specially used to store the jar package of the Tomcat server
  • logs is specially used to store the log information output when the Tomcat server is running
  • temp is specially used to store temporary data generated when Tomcdat is running
  • webapps is specially used to store deployed Web projects.
  • work is the working directory of Tomcat, which is used to store the source code translated from jsp to Servlet when Tomcat is running, and the directory of Session passivation.

5.3 How to start the Tomcat server

Find the startup.bat file under the bin directory under the Tomcat directory, and double-click it to start the Tomcat server.

How to test that the Tomcat server starts successfully? ? ?

Open the browser and enter the following address in the browser address bar for testing:
1. http://localhost:8080
2. http://127.0.0.1:8080
3. http://real ip:8080

When the following interface appears, the Tomcat server starts successfully! ! !
insert image description here

Common startup failures include double-clicking the startup.bat file, and a small black window will appear in a flash.
At this time, the reason for the failure is basically because the JAVA_HOME environment variable is not configured properly.

Configure the JAVA_HOME environment variable:
insert image description here

Common JAVA_HOME configuration errors are as follows:
1: JAVA_HOME must be all uppercase.
Two: JAVA_HOME must be an underscore, not a minus sign -
Three: The JAVA_HOME configuration path only needs to be configured to the jdk installation directory. No need to bring the bin directory.

5.4 Another way to start the tomcat server

1. Open the command line
2. cd to the bin directory of your Tomcat
insert image description here

3. Type in the startup command: catalina run

5.5 Stopping Tomcat

1. Click the x close button of the tomcat server window
2. Set the Tomcat server window as the current window, and then press the shortcut key Ctrl+C
3. Find the shutdown.bat in the bin directory of Tomcat and double-click to stop the Tomcat server

5.6 How to modify the port number of Tomcat

The default port number of Mysql is: 3306
The default port number of Tomcat is: 8080
Find the conf directory under the Tomcat directory and find the server.xml configuration file.
insert image description here

Usually on Baidu: http://www.baidu.com:80

The default port number of the HTTP protocol is: 80

5.7 How to deploy web project to Tomcat

  • The first deployment method: just copy the directory of the web project to the webapps directory of Tomcat.
    1. Create a book project in the webapps directory:
    insert image description here

2. Copy the content of the first stage of the book city made in the morning into it:
insert image description here

3. How to access the web project under Tomcat.
Just enter the access address format in the browser as follows:
http://ip:port/project name/directory/file name
For example: http://192.168.1.100:8080/book/index.html
http://localhost :8080/book/index.html

  • The second deployment method:
    Find the conf directory \Catalina\localhost\ under Tomcat, and create the following configuration file:
    abc.xml The content of the configuration file is as follows:
    Note: when saving, save as encoding if UTF-8
<!-- Context 表示一个工程上下文
path 表示工程的访问路径:/abc
docBase 表示你的工程目录在哪里
-->
<Context path="/abc" docBase="E:\book" />

The path to access this project is as follows: http://ip:port/abc/ means to access the E:\book directory

5.8 The difference between holding the html page to the browser and entering http://ip:port number/project name/access in the browser

  • The principle of holding an html page in hand:
    insert image description here

  • The principle of entering access address access:
    insert image description here

5.9 Access to ROOT projects, and access to the default index.html page

When we enter the access address in the browser address bar as follows:
http://ip:port/ ====>>>> If there is no project name, the default access is the ROOT project.

When we enter the access address in the browser address bar as follows:
http://ip:port/project name/ ====>>>> There is no resource name, and the index.html page is accessed by default

6. IDEA integrates Tomcat server

The operation menu is as follows: File | Settings | Build, Execution, Deployment | Application Servers
insert image description here

Configure your Tomcat installation directory:
insert image description here

You can create a Model to check whether the configuration is successful! ! !

7. Operation of dynamic web projects in IDEA

7.1 How to create a dynamic web project in IDEA

Reference link 1: Create javaweb project configuration Tomacat (IDEA2020.2 version)

Tomcat installation and configuration, and idea2020.2.4 to create a JavaWeb project

7.2 Introduction to the catalog of Web projects

insert image description here

7.3 How to add additional jar packages to dynamic web projects

  • Method 1: Copy the required jar package to lib, then right-click Add as Library, and then select Moudle Library
    insert image description here
    insert image description here
  • Method 2:
    1. You can open the project structure menu operation interface and add your own class library:
    insert image description here

2. Add the jar package files you need for your class library.
insert image description here

3. Select the class library you added and use it for which module:
insert image description here

4. Select the Artifacts option and add the class library to the packaged deployment:
insert image description here

7.4 How to deploy the project in IDEA to run on Tomcat

1. It is recommended to modify the Tomcat running instance name corresponding to the web project:
insert image description here

2. Confirm that your Tomcat instance has the web project module you want to deploy and run:
insert image description here

3. You can also modify the default access address after your Tomcat instance starts:
insert image description here

4. How to run and stop the Tomcat instance in IDEA.

4.1. Start the Tomcat instance normally:
insert image description here

4.2. Start the running instance of Tomcat in Debug mode:
insert image description here

4.3. Stop the running instance of Tomcat:
insert image description here

4.4. Restart the running instance of Tomcat:
insert image description here
insert image description here

7.5 Modify project access path

insert image description here

7.6 Modify the running port number

insert image description here

7.7 Modify the browser used for running

insert image description here

7.8 Configure resource hot deployment

insert image description here

Guess you like

Origin blog.csdn.net/m0_50736744/article/details/125919399