1. Configuration of Tomcat server port
All configurations of Tomcat are placed in the conf folder, and the server.xml file inside is the core configuration file.
If you want to modify the startup port of the Tomcat server, you can modify the port in the Connector node in the server.xml configuration file
For example: Change the startup port of the Tomcat server from the default 8080 to 8081 port
Tomcat server startup port default configuration
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
Modify the Tomcat server startup port to port 8081
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
This will change the original default Tomcat default port 8080 to port 8081. It should be noted that once the *.xml file in the server is changed, the Tomcat server must be restarted, and the new configuration information will be read again after restarting. Because the Tomcat startup port has been modified to 8081 in the server.xml file, the Tomcat server is started with port 8081, as shown in the figure below:
To access the Tomcat server, you must also access it with the new access port: http://localhost:8081/, as shown in the figure below:
2. Mapping method of Tomcat server virtual directory
After the web application is developed, if you want to access the outside world, you need to hand over the directory where the web application is located to the web server for management. This process is called the mapping of virtual directories. So in Tomcat server, how to map virtual directories? There are several ways in total:
2.1. The mapping method of virtual directories 1: Configure in the host element of the server.xml file
Find the host element of the server.xml file, as shown in the following figure:
Adding <Host></Host> to the tags <Context path="/JavaWebApp" docBase="F:/JavaWebDemoProject" /> to map the JavaWebDemoProject JavaWebApp application on the F disk to the virtual directory of JavaWebApp. The virtual directory of JavaWebApp is managed by the Tomcat server. JavaWebApp is a directory that does not exist on the hard disk. It is a directory we write casually, which is a virtual directory, so it is called a "virtual directory". The code is as follows:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context path="/JavaWebApp" docBase="F:/JavaWebDemoProject" /> </Host>
Among them, Context represents a context, which represents a JavaWeb application. The Context element has two attributes.
Ⅰ.path: used to configure virtual directories, and must start with "/".
Ⅱ.docBase: Configure this virtual directory to correspond to the directory where the web application is located on the hard disk.
Use the browser to access the 1.jsp web resource in the virtual directory of "/JavaWebApp". The access result is as follows:
1.jsp can be accessed normally, which means that we have successfully mapped the JavaWebDemoProject JavaWebApp to the virtual directory of JavaWebApp. Accessing "/JavaWebApp/1.jsp" is equivalent to accessing "F:/JavaWebDemoProject/1.jsp"
Note: After Tomcat 6, it is no longer recommended to use the configuration context element in the server.xml file to add virtual directory mapping, because every time the server.xml file is modified, the Tomcat server must be restarted before the server.xml file can be reloaded. There is a description like this in the Tomcat server documentation http://localhost:8080/docs/config/context.html:
It is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes modifying the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.
Individual Context elements may be explicitly defined:
In an individual file at /META-INF/context.xml inside the application files. Optionally (based on the Host's copyXML attribute) this may be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to application's base file name plus a ".xml" extension.
In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context path and version will be derived from the base name of the file (the file name less the .xml extension). This file will always take precedence over any context.xml file packaged in the web application's META-INF directory.
Inside a Host element in the main conf/server.xml.
2.2. The mapping method of virtual directories is 2: Let the tomcat server automatically map
The tomcat server will automatically manage all web applications in the webapps directory and map it into virtual directories. In other words, the web applications in the tomcat server webapps directory can be accessed directly by the outside world.
For example: Copy the JavaWebDemoProject JavaWeb under F disk directly into the tomcat server webapps directory, as shown in the figure below:
At this time, the Tomcat server will automatically map a virtual directory "/JavaWebDemoProject" with the same name for the JavaWebDemoProject JavaWeb application, and then you can use the browser to access the resources of the JavaWeb application, as shown in the figure below:
2.3. The mapping method of virtual directories three
Refer to the Tomcat server documentation:
In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context path and version will be derived from the base name of the file (the file name less the .xml extension). This file will always take precedence over any context.xml file packaged in the web application's META-INF directory.
It means: add a file with xml as the extension in the /conf/Catalina/localhost directory of the tomcat server. The name of the xml file can be arbitrarily, such as the aa.xml below. Note that the sentence "The context path and version will be derived from the base name of the file". The meaning of this sentence is translated as "the path attribute of the context element comes from the name of the xml file". As mentioned above, the path attribute of the Context element is used to configure the name of the virtual directory, so the name of the virtual directory is the name of the xml file.
$CATALINA_BASE refers to the root directory of the tomcat server. [enginename] refers to the engine name used by the Tomcat server. The engine used by Tomcat is Catalina.
Add Context element mapping JavaWeb application in the aa.xml file, the code is as follows:
<Context docBase="F:/JavaWebDemoProject" />
Note: The path attribute is not specified in the Context element to set the name of the virtual directory. Then the name of the virtual directory mapped by "F:/JavaWebDemoProject" is Shenma, which is the name of the xml file currently being edited.
The biggest advantage of using this method to map virtual directories is that you do not need to restart the Tomcat server after modifying the configuration file. For example, modify aa.xml to bb.xml. The Tomcat server will automatically Undeploying context [/aa], and then automatically information: Deploying configuration descriptor D:/apache-tomcat-7.0.53/conf/Catalina/localhost/bb.xml
3. Tomcat server configuration virtual host
3.1. Configure the virtual host
Configuring a virtual host is to configure a website.
To configure a virtual host (website) on the Tomcat server, you need to modify the server.xml configuration file under the conf folder, use the Host element to configure it, open the server.xml, and you can see a virtual host (website) named localhost on the Tomcat server, as shown in the figure below:
Usually, we put the developed JavaWeb application in the webapps folder, and then we can access it using the "http://localhost:port number/JavaWebAppName". In fact, we are accessing the virtual host (Host) whose name is "localhost". This virtual host manages all web applications in the webapps folder.
For example: http://localhost:8080/JavaWebDemoProject/1.jsp. This URL address accesses the 1.jsp web resource in the JavaWebDemoProject application under the virtual host named localhost.
We can configure a virtual host in the following way, for example:
<Host name="www.gacl.cn" appBase="F:/JavaWebApps"> </Host>
Here we have a new configuration of a virtual host. The name of the virtual host is "www.gacl.cn". The virtual host "www.gacl.cn" now manages all web applications under the JavaWebApps folder. Usually, when we use the domain name "www.baidu.com" on the Internet to visit Baidu's website, we are actually visiting a virtual host with the name "www.baidu.com". So when we want to access the virtual host with the name "www.gacl.cn", we can use the "domain name (www.gacl.cn)" to access it. Note that appBase="F:/JavaWebApps". The JavaWebApps folder here does not represent the root directory of a project, but a folder that stores one or more JavaWeb applications, as shown in the figure below:
It's like the webapps folder of the Tomcat server, which stores a lot of JavaWeb applications.
3.2. Register the domain name in the windows system
If the configured host (website) wants to be accessed externally through the domain name, you must register the domain name used when accessing the website in the DNS server or Windows system and find the hosts file in the "C:/Windows/System32/drivers/etc" directory, as shown in the figure below:
Edit this file and bind the domain name and IP address of the newly added website together, so that we can use the domain name www.gacl.cn in the browser to access the web applications managed in the virtual host name www.gacl.cn.
Use the browser to access the 1.jsp web resource under the JavaWebDemo1 web application under the domain name "www.gacl.cn" through the domain name "www.gacl.cn". The virtual host "www.gacl.cn" opens a 8080 port. Users can only access the 1.jsp web resource under the JavaWebDemo1 web application through this 8080 port to access the 1.jsp web resource under the JavaWebDemo1 web application through this 8080 port
4. The process of interaction between the browser and the server
4.1. Browser and server interaction diagram
Browser and server interaction diagram
When we open the browser and enter the URL address "http://www.gacl.cn:8080/JavaWebDemo1/1.jsp" in the browser's address bar to access the 1.jsp web resource on the server, the browser and the server did a magic operation. How do we see the contents of the 1.jsp web resource in the browser?
The browser and server do the following:
1. The browser searches the hostname corresponding to the hostname in the Hosts file of the operating system based on the hostname "www.gacl.cn".
2. If the browser does not find the corresponding IP address in the operating system's Hosts file, go to the DNS server on the Internet to find the corresponding IP address of the host "www.gacl.cn".
3. After the browser finds the corresponding IP address of the host "www.gacl.cn", it uses the IP address to connect to the web server.
4. After the browser connects to the web server, it uses the http protocol to send a request to the server. During the process of sending the request, the browser will transmit data to the web server in the form of a Stream, telling the web server which web application web resource to access, as shown in the figure below:
This is the data transmitted to the server when the browser sends a request to the web server. Let's explain the content of "GET /JavaWebDemo1/1.jsp HTTP/1.1".
GET: Tell the web server that the browser sends requests to the server in GET.
/JavaWebDemo1/1.jsp: Tell the web server that the browser wants to access the 1.jsp web resource in the JavaWebDemo1 application.
HTTP/1.1: Tells the web server that the browser requests it using the HTTP protocol and uses the 1.1 version.
5. After the browser completes the above 4 steps, it starts waiting, waiting for the web server to transfer the 1.jsp web resource it wants to access to.
6. After the server receives the data transmitted by the browser, it begins to parse the received data. When the server parses the contents in "GET /JavaWebDemo1/1.jsp HTTP/1.1", it knows that the client browser wants to access the 1.jsp Web resource in the JavaWebDemo1 application. Then the server reads the contents in the 1.jsp Web resource and transmits the read contents to the browser in the form of a Stream, as shown in the figure below:
This is the data transmitted to the browser by the web server.
7. After the browser gets the data transmitted to it by the server, it can display the data to the user, as shown in the figure below:
The "JavaWebDemo1" I saw is the effect of the browser parsing the data sent back by the server.
Data sent back by the server:
HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Type: text/html;charset=ISO-8859-1Content-Length: 102Date: Mon, 19 May 2014 14:25:14 GMT<html> <head> <title>JavaWebDemo1</title> </head> <body> JavaWebDemo1 </body></html>
This is the interaction process between the browser and the server.
5. The composition structure of JavaWeb applications
When developing JavaWeb applications, different types of files have strict storage rules. Otherwise, it may not only make the web application inaccessible, but also cause the web server to start an error.
WebRoot → The directory where the web application is located. Generally, the virtual directory must be configured into this folder.
┝WEB-INF: This folder must be located in the WebRoot folder, and must be named in this form, with letters required to be capitalized.
┝web.xml: Configuration file, with format requirements, this file must be named in this form and must be placed in the WEB-INF folder.
The format of web.xml can be directly referenced from Tomcat: find the web.xml file in the webapps/ROOT/WEB-INF directory in the Tomcat directory, copy the file to our newly created WEB-INF folder, and modify the web.xml file, delete the comments inside, leaving only the code shown below:
web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description></web-app>
This is the format of the web.xml file.
The above is all about this article, I hope it will be helpful to everyone's learning.