简体   繁体   中英

Tomcat set application context using configuration (not war name)

My question is this - Say I have a war file called my-app-123.war. I want to deploy it on a Tomcat server (9.0.x), let it auto unpack.

The application would then be accessible by http://localhost:8080/my-app-123

Is there a way, without renaming the war file, to make the application accessible from http://localhost:8080/my-app ?

I will preface this by saying I realize the easiest solution is to just name the war file. I'm curious if there is a way to do this in Tomcat configurations.

I did do this already (inside the host section of my server.xml file):

<Context path="/my-app" docBase="my-app-123"></Context>

But I read this online ( https://tomcat.apache.org/tomcat-7.0-doc/config/context.html ) in the path variable description:

Even when statically defining a Context in server.xml, this attribute must not be set unless either the docBase is not located under the Host's appBase or both deployOnStartup and autoDeploy are false. If this rule is not followed, double deployment is likely to result.

And I can access the app now at http://localhost:8080/my-app and http://localhost:8080/my-app-123 , which isn't a real solution.

The following works for Tomcat deployments I have used - there are no double-deployment issues.

In the example I will use here, I have a simple "hello world" webapp in TomcatDemo-1.0-SNAPSHOT.war . It is deployed on Tomcat 9.0 in the standard location ( webapps directory).

I want the application's context path to be /my-foo-app .

I use the following context entry in server.xml :

<Context path="/my-foo-app" docBase="TomcatDemo-1.0-SNAPSHOT.war"></Context>

I also use the following in server.xml :

<Host name="localhost" 
      appBase="webapps" 
      deployOnStartup="true"
      deployIgnore="TomcatDemo-1.0-SNAPSHOT.war" 
      unpackWARs="true" 
      autoDeploy="false">

The important item here is deployIgnore . It is described here:

https://tomcat.apache.org/tomcat-9.0-doc/config/host.html#Automatic_Application_Deployment

When using automatic deployment, the docBase defined by an XML Context file should be outside of the appBase directory. If this is not the case, difficulties may be experienced deploying the web application or the application may be deployed twice. The deployIgnore attribute can be used to avoid this situation.

Alternatively, if you don't mind about automatic start-up deployments, you can set deployOnStartup="false" - in which case you don't need deployIgnore .

In these scenarios, the web app is available only here:

http://localhost:8080/my-foo-app/   <-- OK

Otherwise, as you note, with double-deployment the web app would also be available here (and you would see two exploded directories in webapps ):

http://localhost:8080/TomcatDemo-1.0-SNAPSHOT/  <-- BAD!

Hope that helps.

Finally, it can get a little complicated, with all the various auto-deployment options. This page provides a set of summary tables and explanations:

https://tomcat.apache.org/tomcat-9.0-doc/config/automatic-deployment.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM