Tomcat: Web server software

Tomcat is written in the pure Java language

The installation

Download tomcat.apache.org/download-80…

No installation, decompress the package.

Note: It is recommended not to have Chinese characters and Spaces in the installation directory

The directory structure

Start the

  • Windows: Run./bin/startup.bat

  • Linux: Run./bin/startup.sh

Possible problems

Chinese garbled

For details, see blog post [Chinese garbled characters at Tomcat startup]

The black window flashed by

  • Cause: The JAVA_HOME environment variable is not configured correctly
  • Solution: Configure the JAVA_HOME environment variable correctly

Start the error

violence

  • Find the occupied port number, find the corresponding process, and kill the process
  • cmd>netstat -ano> View process ID (PID) > Task Manager (View – Select Column-PID) > End Process (corresponding PID)

gentle

  • Example Modify the port number of itself
  • ./conf/server.xml

  • <Connector port="8888" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8445"

  • Generally, the default port number of Tomcat is changed to 80. Port 80 is the default HTTP port number.

    • Benefits: When accessing, you do not need to enter a port number
  • In this mode, multiple Tomcat servers can coexist in a single machine

access

Browser input:

  • Visit yourself: http://localhost:8080
  • Access others: http://ip address of others :8080

Viewing the IP Address

  • cmd>$ ipconfig> the IPv4 address

Shut down

  • For the server itself, use ** “Normal Shutdown” **
  • However, Tomcat is used with IDEA in the later stage

Normally closed

  • There are two ways:
  1. Manual operation./bin/shutdown.bat
  2. In the Tomcat window, pressCtrl+C

closed

  • Click the X in the upper right corner of the Tomcat window

How the project is deployed/configured

  • There are three ways:
  1. Put the project directly intowebappsIn directory
  2. configurationconf/server.xmlfile
  3. inconf\Catalina\localhostCreate an XML file with any name

Virtual directory

When a browser needs to access an item through http://localhost:8080, it follows it with a/directory or path (called a virtual directory)

Simply place the project in the Webapps directory

  • / Project (folder) name: Indicates the access path of the projectVirtual directory

Simplify deployment

  • The first approach also has a form of simplified deployment
  • Put the project into a WAR package and place the WAR package in the Webapps directory
  • The WAR package is automatically decompressed
  • To delete the project, delete the corresponding WAR package. The corresponding production project folder is automatically deleted.

Configure the conf/server. XML file

  • in<Host>The value is configured in the label body
  • <Context docBase="D:\hello" path="/hehe" />
  • DocBase: The path where the project is stored
  • Path: indicates a virtual directory
  • Disadvantages: Need to modify the configuration file, not very secure

Create an XML file with any name in conf\Catalina\localhost

  • Write it in a file
  • <Context docBase="D:\hello" />
  • Virtual directory: The name of the XML file. Such asLocalhost: 8080 / XML file/index. HTML
  • Hot deployment: After the file name is deleted or changed, the service does not need to be restarted.

Type of deployment project

  • Static project
  • The dynamic project

Static project

  • Only static resources can be stored
  • HTML, CSS, JavaScript, images, audio, video, text, and more

The dynamic project

  • It can store not only static resources, but also dynamic resources

Directory structure (Java dynamic Project)

Project root directory Directory directory WEB. XML: the core configuration file of a WEB project Directory directory classes: directory for storing bytecode files Directory lib: directory for storing dependent JAR packagesCopy the code

Such as:

IDEA is used together with Tomcat

Integrate into IDEA

New Web Project

Run the project

Note:

  1. War is deployed packaged by default

  2. / : The default virtual directory sets the current project to the root directory

Other configuration

  • The Tomcat server is not restarted when the project is redeployed