Warm tip: This article is reproduced from serverfault.com, please click

Setting URL with subfolder in Tomcat 9

发布于 2020-12-06 10:28:49

I have Tomcat 9. I am trying to get the URL lik this: http://default.local:8080/default_war/ but my site is still available only by http://default.local:8080. The address I am going to set is also generated by Intellij in Run/Configuration so it shoud be possible to set it. Of course I could edit it in Intellij but I wonder how to achieve URL subderictories in Tomcat 9.

What should I change in my code? Here there are my setting files:

$ cat /home/u/opt/tomcat/conf/server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="default.local"  appBase="default"
            unpackWARs="true" autoDeploy="true">
        <Context path="/default_war" />
        <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>


    </Engine>
  </Service>
</Server>
u@i3 ~/opt/tomcat

$ cat conf/Catalina/default.local/ROOT.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="${catalina.base}/deploy/default_war.war">
</Context>
u@i3 ~/opt/tomcat
$ 
Questioner
trzczy
Viewed
0
andrewjames 2020-12-07 03:14:18

This is happening because you are using a context file called ROOT.xml.

The ROOT application is the one which is served from the base URL - in your case, http://default.local:8080.

Depending on exactly how you deployed your WAR, you will need to take the following steps:

1 - Stop Tomcat and change the name of the ROOT.xml file to default_war.xml.

The above step means you are no longer using a ROOT-based webapp (see final note below for more on that).

2 - Because you have specified appBase="default" in your server.xml, you may also need to go to that default folder and remove any sub-folders in there, before restating Tomcat.

I would expect there to be a ROOT folder in there, because you have already used a ROOT.xml context file. You don't want that folder, because that will continue to serve your webapp from the base URL.

When you restart Tomcat, you should see a new folder called default_war in the default folder - and that is where your application will be served from - and it should be available only at http://default.local:8080/default_war/.

Background note: In Tomcat, ROOT is something of a special case. When you download and install a fresh installation of Tomcat, you will see a folder called ROOT in the webapps directory. This is where the main Tomcat welcome/congratulations page is served from - and like your ROOT.xml content, it is served from the base URL. You can see some of the possible configurations in the "naming" table shown in this Tomcat documentation page.