Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why do web projects configure virtual directories?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about why web projects should be configured with virtual directories. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Why configure a virtual directory for a web project?

When you start to learn JavaWeb, you will find that as long as we put the web project in Tomcat's webapps directory, and then through the http://localhost:8080/ project name / a page can access the site. However, this is not in line with the specification, it is best to separate the web project file from the Tomcat installation file. So how to achieve this requirement? Of course, configure the virtual directory for the web project.

How do I configure a virtual directory for a web project?

As mentioned earlier, we need to separate the project file from the Tomcat installation file. Suppose we put the test project here in the root directory of disk D. There are officially five ways to configure virtual directories, but only two are introduced here.

Method one

Add the Context tag under the Host tag of the server.xml file in the conf directory, as shown below, so that you only need to enter: http://localhost:8080/test/index.html in the address bar to access the index page of the test project. But the disadvantage of this approach is that the server needs to be restarted to take effect.

Method two

Create a new XML file named test1 in the conf\ Catalina\ localhost directory and write the contents on it. This method can take effect immediately without restarting Tomcat. Type: http://localhost:8080/test1/index.html in the address bar to access the index.html file of the web project. The pathname of the virtual directory can be arbitrary and does not 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 the content that needs to be changed in the XML configuration file, and then looks for this information when the program is running, so that the program can be changed without modifying the source code of the program. Configuration files are often encountered in future learning.

How to deploy the website to the cloud so that others can also access the site?

Now you have purchased the CVM and domain name, and have done the resolution. What should you do if you want to access your website through your domain name www.jb51.net?

You need to create a new tag under the tag in the server.xml file, as follows:

Then change the first label in server.xml to the following, that is, change port 8080 to 80

To sum up, a new host (Host) is created, and the name of the host is www.jb51.net. Because the http protocol defaults to port 80, there is no need to specify the port number. When no path is given, the test project under disk D will be accessed by default. As for the page that visits the test project, it needs to be specified.

Generally speaking, if you need to add the following code to the web.xml file of the corresponding web project, you will access the index.html page by default, but the web.xml file of Tomcat has been written, so you don't need to specify it separately.

Index.html, thank you for your reading! This is the end of the article on "Why web project configuration virtual directory". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report