What is a Tomcat

Tomcat is simply a web server running JAVA. At the bottom is a Socket program. It is also a container for JSP and Serlvet.


Why do we need Tomcat

If you have learned HTML, CSS, you will know that your pages can only be accessed by yourself, others can not remotely access your pages, Tomcat is a program that provides the ability to let others access your pages


Configure Tomcat

JDK support is required to run Tomcat (Tomcat will find the required JDK through JAVA_HOME).

  • Create a JAVA_HOME environment variable.

  • Go to bin in the Tomcat directory to start startup.bat. The following page is displayed.

  • Enter http://localhost:8080 in the browser address bar. If the Tomcat page is displayed, the configuration is successful.

Error uild-style Web Application Directory web222 Error uild-style Web Application directory web222 Currently, JDK8 does not match Tomcat7.0, so we need to lower the JDK version.


Of course, there may be difficulties in directly starting Tomcat. If Tomcat cannot be started normally, the following situations will occur

  1. The JAVA_HOME configuration is incorrect
  2. The port is occupied
    • First, enter netstat -anb in CMD to see who is using the port (Tomcat defaults to 8080) and close it in the process
    • Conf /server. XML file in the tomcat home directory. Change port 8080 to 8088 or some other port.

Introduction to related terms


Tomcat Structure Directory

A brief introduction to the directory

  1. Bin: starts and closes the Bat file of Tomcat

  2. Conf: indicates the configuration file

    • server.xmlThis file is used to configure server-related information, such as the port number for tomcat to start, and configure the Host.
    • web.xmlFile configuration and Web Application (Web application is equivalent to a Web site)
    • tomcat-user.xmlConfigure user names, passwords, and permissions.
  3. Lib: This directory houses the JAR packages needed to run tomcat

  4. Logs: Stores logs. When we need to view logs, we can query information

  5. Webapps: Place our Web applications

  6. Work directory: This directory is used to store the corresponding server files and. Class files generated after the JSP is accessed


Details of the Webapps directory

Create a web1 directory in webapps, place our HTML files, JSP files, images, etc., and then web1 is managed as a Web application.

Example: Create a Web site under Webapps, create an HTML file under the Web site, access the HTML file


Web site catalogs are regulated

Why set up a Web site directory like this?

  • Requirement: I have multiple HTML files and want to use one of them as the front page of my Web site.
  • Without the support of the web.xml file in the WEB-INF directory, I could not solve my requirements
  • This specification is a convention.

Use the helloword2.xml file under the Web site as the first page of the site

  • Create a web-INF directory

  • Create a web.xml file in the web-INF directory

  • Web.xml is not possible to write, so you can copy it from another site in the webapps directory.

  • Add the following code to web.xml


	  <welcome-file-list>
	        <welcome-file>helloword2.html</welcome-file>
	  </welcome-file-list>

Copy the code

  • Helloword2.html is already the first page of the Web site, so there is no need to specify resource access.


Configuring a Virtual Directory

Why do I need to configure a virtual directory?

  • If you put all web site directories under Webapps, you may run out of disk space and make it difficult to manage web site directories (if there are many web site directories)
  • By default, only webapps directories can be automatically managed by Tomcat as a Web site.
  • The web server manages the directory where the Web application resides. This process is called virtual directory mapping

Method 1:

  • Create a Web site directory under the other drive letters, and create the WEB-INF directory and an HTML file.

  • Find the /conf/server. XML file in the Tomcat directory

  • Add the following code under the node in server.xml. Path represents the Web project name entered when accessing, and docBase represents the absolute path to the site directory

		<Context path="/web1" docBase="D:\web1"/>
Copy the code

  • Access the configured Web site


Method 2 for configuring a virtual directory:

  • Go to conf\Catalina\localhost and create an XML file whose name is the name of the site.

  • The code for the XML file is as follows. DocBase is the absolute path to your Web site

<? xml version="1.0" encoding="UTF-8"? > <Context docBase="D:\web1" 
    reloadable="true"> 
</Context> 
Copy the code
  • Access HTML resources under the Web site

Configuring a Temporary Domain Name

There are several ways to access the Tomcat server

  • Use the localhost domain name to access
  • Access using IP address 127.0.0.1
  • Access by machine name [local machine or LAN only]
  • Access using the local IP address [Enter ipconfig in CMD to query the local IP address]
  • You can also configure a temporary domain name for your machine

Procedure For configuring a temporary domain name

  • Go to C:\Windows\System32\drivers\etc and find the hosts file

  • Configure the temporary domain name in the hosts file


Setting a Virtual Host

What is a virtual host?

Multiple sites with different domain names coexist in one Tomcat

Why do I need a virtual host?

Example: I am currently developing 4 websites with 4 domain names. If I don’t configure a web host, and one Tomcat server runs one site, I need four computers to run four sites.

Steps for configuring a virtual host

  • Add the host name to the Tomcat server. XML file

	  <Host name="zhongfucheng" appBase="D:\web1">
					<Context path="/web1" docBase="D:\web1"/>
	  </Host>

Copy the code

  • Access the Web site under the virtual host

Tomcat architecture


Flowchart of a browser accessing WEB resources


practice

Enter HTTP //: Zhongfucheng in the browser to display directly to the page

Analysis:

  1. Virtual hosts need to be configured
  2. Change port 8080 to 80
  3. Set the home page of the Web site
  4. The mapped virtual directory is /

Open source project (5.8K STAR) :Github.com/ZhongFuChen…

If you want to follow my updated articles and shared dry goods in real time, scan the QR code or wechat search Java3y, and reply “888” under the official account to get it!!

  • 🔥Java beautiful brain map
  • 🔥Java Learning path
  • 🔥 Common Java development tools
  • 🔥 Original PDF ebook

The content of the PDF document is typed by hand. If you don’t understand anything, you can ask me directly (the official account has my contact information).