In Tomcat how do You configure virtual directories for web projects

  • 2020-06-19 12:12:09
  • OfStack

Why configure the virtual directory for the web project?

When you first learn JavaWeb, you will find that you can access the site by placing the web project in Tomcat's webapps directory and using http://localhost:8080/ project name/a page. However, this is not the norm and it is best to separate the web project files from the Tomcat installation files. So how do you do that? Configure virtual directories for the web project, of course.

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

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

Method 1

Add the Context tag under the Host tag of the ES27en.xml file in the conf directory < Context path="/test" docBase="D:\test"/ > , after the change as shown below, so only need to input in the address bar: http: / / localhost: 8080 / test index html can access index test projects page. The downside of this approach is that you need to restart 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.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->
    <Context path="/test" docBase="D:\test"/>
    <!-- Access log processes all example.
       Documentation at: /docs/config/valve.html
       Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
        prefix="localhost_access_log" suffix=".txt"
        pattern="%h %l %u %t &quot;%r&quot; %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"/ > Just fine. This method takes effect immediately without restarting Tomcat. In the address bar enter: http: / / localhost: 8080 / test1 index html can access web project index. html file. 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 the XML file? This is because the Tomcat program writes these changes in the XML configuration file, and then looks for this information when the program is running, in order to be able to change the program without changing the source code. In the future learning often encounter configuration files oh!

How do you deploy your website in the cloud so that others can access it?

Now that you have purchased the cloud server and domain name, you have resolved it. What should you do if you want to access your website via your domain name www.ofstack.com?

You need the file in server.xml < Engine > Create a new one under the label < Host > Tag, which reads as follows:


<Host name="www.ofstack.com" appBase="webapps"
      unpackWARs="true" autoDeploy="true">
    <Context path="" docBase="D:\test"/> 
</Host>

Then put the first in server.xml < Connector > Change the label to look like this, i.e. change port 8080 to 80


<Connector port="80" protocol="HTTP/1.1"
        connectionTimeout="20000"
        redirectPort="8443" />

Summary 1, is a new host (Host), named www.ofstack.com, http protocol default USES 80 port, so there is no need to specify the port number, in no given path, the default access to test D disk, as for the access to the test project page this also needs to be specified.

1 Generally, you need to add the following code to the web.xml file corresponding to web project, and you will visit the page of ES112en.html by default, but the web.xml file of Tomcat is already written, so you don't need to specify otherwise.


<welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

Originally need https: / / www ofstack. com: / project name/front page 8080 access to the home page, now only need through www. ofstack. com can access.


Related articles: