Why configure a virtual directory for a web project?
When you are new to JavaWeb, you will find that as long as we put the web project in Tomcat's webapps directory, and then access the website through http://localhost:8080/project name/a page. However, this does not comply with the specifications, so it is best to separate the web project file and the Tomcat installation file. So how to achieve this requirement? Of course, it is to configure a virtual directory for web projects.
How to configure a virtual directory for a web project?
As mentioned earlier, we need to separate the project file and the Tomcat installation file. Suppose we place the test project in the root directory of disk D. There are five methods for configuring virtual directories, and only two are introduced here.
Method 1
Add the Context tag <Context path="/test" docBase="D:\test"/> under the Host tag of the file in the conf directory. The modification is as follows. In this way, you only need to enter: http://localhost:8080/test/ in the address bar to access the index page of the test project. However, the disadvantage of this method is that it requires restarting the server to take effect.
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/ --> <!-- <Valve className="" /> --> <Context path="/test" docBase="D:\test"/> <!-- Access log processes all example. Documentation at: /docs/config/ Note: The pattern used is equivalent to using pattern="common" --> <Valve className="" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host>
Method 2
Create a new XML file named test1 in the conf\Catalina\localhost directory, and write the contents <Context docBase="D:\test1"/>. This method can take effect immediately without restarting Tomcat. Enter: http://localhost:8080/test1/ in the address bar to access the web project file. The path name of the virtual directory can be arbitrary and does not necessarily have to be the same as the project name.
Why do both methods work by modifying this XML file? This is because the Tomcat program writes this content that needs to be changed in the XML configuration file, and then looks for this information when the program is running, so as to achieve the purpose of changing the program's running without modifying the program source code. You will often encounter configuration files in your future study!
How to deploy the website in the cloud so that others can also access this website?
Now you have purchased the cloud server and domain name and have done a good job of resolving it. You want to pass your domain nameWhat should I do when visiting your website?
You need to create a new <Host> tag under the <Engine> tag of the file, with the following content:
<Host name="" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="D:\test"/> </Host>
Then change the first <Connector> tag in , which is to change port 8080 to 80
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
To sum up, a new host (Host) is created with the host name, because the http protocol uses port 80 by default, there is no need to specify the port number. When no path is given, the test project under D disk is accessed by default. As for the page to access the test project, this still needs to be specified.
Generally, you need to add the following code to the corresponding web project file, and the page you will access by default, but the Tomcat file has been written, so there is no need to specify it separately.
<welcome-file-list> <welcome-file></welcome-file> </welcome-file-list>
It turns out that I need to passhttps://:8080/Project name/home page name to access the home page, now only need to passYou can access it.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.