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
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]
- Start startup.bat in bin under Tomcat directory, the following is the page for launching Tomcat successfully.
- 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 Tomcat
Error 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:
- Java home configuration error
- 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‘
Introduction to relevant terms
Tomcat structure directory
A brief introduction to the catalog
- Bin: start and close the bat file of Tomcat
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
- Lib: this directory places the jar package needed to run Tomcat
- Logs: store logs. When we need to view logs, we can query information
- Webapps: placing our web applications
- 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
The directory of Web site is standardized
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
- Create a web.xml in the WEB-INF directory
- 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
<welcome-file-list> <welcome-file>helloword2.html</welcome-file> </welcome-file-list>
- Visit the web site【Helloword2.html is the homepage of the web site, so you don’t need to specify resource access】
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.
- Find the / conf / server.xml file in the Tomcat directory
- 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"/>
- Visit the configured web site
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.
- 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"?> <Context docBase="D:\web1" reloadable="true"> </Context>
- Visit the HTML resources under the web site
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 127.0.0.1 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
- Configure the temporary domain name under the hosts file
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"/> </Host>
- Visit the web site under the virtual host
Flow chart of browser accessing web resources
Enter http / / in the browser: zhangfucheng directly display to the page
- Virtual host configuration required
- Change port 8080 to 80
- Set Web site home page
- Map virtual directory as/
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