Tomcat is so simple


What is Tomcat

Tomcat is simply a network server running Java. The bottom layer is a socket program, which is also a container of JSP and servlet.

Why do we need Tomcat

If you have learned HTML and CSS, you will know that the pages you write can only be accessed by yourself,People can’t access your pages remotelyTomcat isProvide a program that allows others to access the page they write

Tomcat is so simple

Configure Tomcat

Running Tomcat requires the support of JDK [Tomcat will find the required JDK through Java [home].

  • New Java home environment variable[path is the home directory of JDK]

Tomcat is so simple

  • Start startup.bat in bin under Tomcat directory, the following is the page for launching Tomcat successfully.

Tomcat is so simple

  • In the browser address field, enter http: / / localhost: 8080, ifThe Tomcat page appears, indicating that the configuration is successful

Be careful:If it appears when using TomcatError deploying web application directory web222, because the versions of JDK and Tomcat do not match. At present,Jdk8 cannot match tomcat7.0SoTo reduce the JDK version[change to JDK7]

Of course, it may not be easy to start Tomcat directly,If Tomcat cannot be started normally, the following conditions will occur:

  1. Java home configuration error
  2. Port occupied

    • The first method: enter netstat – anb in CMD to see who has occupied the port [Tomcat defaults to 8080], and close it in the process
    • The second way is to actively change the port of Tomcat,Modify it in the conf / server.xml file under the Tomcat home directory, change port 8080 to 8088 or something else‘
    • Tomcat is so simple

Introduction to relevant terms

Tomcat is so simple

Tomcat structure directory

Tomcat is so simple

A brief introduction to the catalog

  1. Bin: start and close the bat file of Tomcat
  2. Conf: configuration file

    • server.xmlThis file is used to configure server related information, such as the port number of Tomcat startup and host configuration
    • web.xmlFile configuration and web application (web application is equivalent to a web site)
    • tomcat-user.xmlConfigure user name password and related permissions
  3. Lib: this directory places the jar package needed to run Tomcat
  4. Logs: store logs. When we need to view logs, we can query information
  5. Webapps: placing our web applications
  6. Work working directory: this directory is used to storeAfter JSP is accessed, the corresponding server file and. Class file are generated

Detailed description of webapps directory

Web1 directory is established in webappsOur HTML files, JSP files, pictures, etc,Web1 is managed as a web application[only supported after Tomcat 6.0]

Example:Create a web site under webapps, create an HTML file under the web site, and access the HTML file

Tomcat is so simple

The directory of Web site is standardized
Tomcat is so simple

Why set up the web site directory like this?

  • Requirement: I have multiple HTML files and want to use one of them as the homepage of my web site.
  • IfWithout the support of web.xml file in the WEB-INF directory, I can’t solve my needs
  • This specification is a convention.

The helloword2.xml file under the web site is used as the first page of the site

  • Create a new WEB-INF directory

Tomcat is so simple

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

Tomcat is so simple

  • We can’t write web.xml, so we canCopy it from other sites in the webapps directory[copy the file of root / WEB-INF / web.xml to your own site]
  • Add the following code to web.xml


Tomcat is so simple

  • Visit the web site【Helloword2.html is the homepage of the web site, so you don’t need to specify resource access

Tomcat is so simple

Configure virtual directory

Why do I need to configure virtual directories?

  • If the directories of all web sites are placed under webapps, it may causeInsufficient disk spaceAlsoNot conducive to the management of Web site directory[if there are many web site directories]
  • holdIf the directory of a web site is distributed to other disks for management, you need to configure a virtual directory (by default, only the directory under webapps can be automatically managed as a web site by Tomcat)
  • This process is called virtual directory mapping

Configure virtual directory method 1:

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

Tomcat is so simple

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

Tomcat is so simple

  • Add the following code under the < host > node in server.xml.Path refers to the web project name entered during access, and docbase refers to the absolute path of the site directory

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

Tomcat is so simple

  • Visit the configured web site

Tomcat is so simple

Configure virtual directory method 2:

  • Go to the conficatalinalocalhost file and create an XML file,The name of the file is the name of the site.

Tomcat is so simple

  • The code of the XML file is as follows,Docbase is the absolute path to your web site
<?xml version="1.0" encoding="UTF-8"?> 
  • Visit the HTML resources under the web site

Tomcat is so simple

Configure temporary domain name

There are several ways to access Tomcat server

  • Use the localhost domain name to access [localhost represents the local machine]
  • Use IP address to access [the IP address is also local]
  • Use the machine name to access [local or local area network only]
  • Access using native IP address【Enter ipconfig in CMD to query the local IP address
  • You can also configure a temporary domain name for the machine

To configure a temporary domain name

  • Open to C: windowssystem32driversetc, and find the hosts file

Tomcat is so simple

  • Configure the temporary domain name under the hosts file

Tomcat is so simple

Set up virtual host

What is a virtual host?

Multiple websites with different domain names coexist in one Tomcat

Why do I need a virtual host?

Example: I have now developed 4 websites with 4 domain names. If I don’t configure a virtual host and a Tomcat server runs a website, I need four computers to run four websites.

To configure a virtual host

  • Add the host name in the server.xml file of Tomcat

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

Tomcat is so simple

  • Visit the web site under the virtual host

Tomcat is so simple

Tomcat architecture

Tomcat is so simple

Flow chart of browser accessing web resources

Tomcat is so simple


Enter http / / in the browser: zhangfucheng directly display to the page


  1. Virtual host configuration required
  2. Change port 8080 to 80
  3. Set Web site home page
  4. Map virtual directory as/

Tomcat is so simple

If there is something wrong in the article, you are welcome to correct it and communicate with each other. Students who are accustomed to reading WeChat technical articles can pay attention to WeChat official account: Java3y