issue with tomcat context setting in server.xml - java

I am using tomcat 7.
When I set context different from war file name, everything works fine.
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="../webapps/abc.war" path="/def" reloadable="true" />
</Host>
But at tomcat startup I see two exploded folder abc and def.
Please help if anybody knows about this issue resolution.
Thanks.

From the Tomcat doc (see "path" attribute):
Even when statically defining a Context in server.xml, [the path] 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.
This is a bit obscur, but my understanding is that you need to use an xml context file to achieve what you are trying to do. How to define a context config file is documented in the above link.
A simpler fix would be to just rename your war file to def.war.

Related

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

Deploying War. Tomcat not taking context.xml from META-INF in consideration

I do have a Spring Boot Application which uses custom context.xml for the tomcat.
The context.xml contains property, defining the spring active profile
<Context>
<Environment name="spring.profiles.active" value="profileName" type="java.lang.String" override="false" />
</Context>
File location is the /src/main/webapps/META-INF
I was expecting that after the file is deployed to the tomcat, context xml will be automatically picked by tomcat and thrown to the conf/catalina/localhost/
As it turned out, the war was deployed, but the conf/catalina/localhost stayed empty.
After reading the docs i've found out that the server.xml has to be updated with the copyXML parameter as Host container.
The documentation says:
copyXML
Set to true if you want a context XML descriptor embedded inside the application (located at /META-INF/context.xml) to be copied to xmlBase when the application is deployed. On subsequent starts, the copied context XML descriptor will be used in preference to any context XML descriptor embedded inside the application even if the descriptor embedded inside the application is more recent. The flag's value defaults to false. Note if deployXML is false, this attribute will have no effect.
My Host looks like
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true" copyXML="true">
</Host>
After restarting the server and redeploying the app, the /conf/Catalina/localhost still stayed empty.
Do you have any suggestions, what actions have to be taken in order to use custom context.xml?
I've figured it out.
My build script did not copy the file correctly, the file name was not "context.xml" but "appName.xml" and tomcat didn't pick it up.

Tomcat issues with Host, Contexts and webapp double instances

/...too long and unclear explanation.../
In other way, I search the correct way to reach the site with just (one or more) domain name (then www.my_domain.it and not www.my_domain.it/appname), and all of these must point to the same instance of the webapp (so that from any domain name you access, the same Runtime data will always be displayed).
So, yes I have deployed the WAR file in tomcat/webapps folder, now, the correct to edit the conf/server.xml to get the result mentioned above, corresponds to one of the following?
1.
<Host name="my_domain.eu" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Alias>www.my_domain.eu</Alias>
<Alias>www.my_domain.it</Alias>
<Alias>www.my_domain_2.it</Alias>
<Context path="" docBase="appname" debug="0" privileged="true" />
...
</Host>
2.
<Host name="my_domain.eu" appBase="webapps/appname" unpackWARs="true" autoDeploy="true">
<Alias>www.my_domain.eu</Alias>
<Alias>www.my_domain.it</Alias>
<Alias>www.my_domain_2.it</Alias>
<Context path="" docBase="appname" debug="0" privileged="true" />
...
</Host>
3.
<Host name="my_domain.eu" appBase="webapps/appname" unpackWARs="true" autoDeploy="true">
<Alias>www.my_domain.eu</Alias>
<Alias>www.my_domain.it</Alias>
<Alias>www.my_domain_2.it</Alias>
<Context path="/appname" docBase="appname" debug="0" privileged="true" />
...
</Host>
4.
<Host name="my_domain.eu" appBase="webapps/appname" unpackWARs="true" autoDeploy="true">
<Alias>www.my_domain.eu</Alias>
<Alias>www.my_domain.it</Alias>
<Alias>www.my_domain_2.it</Alias>
<!-- without context definition -->
...
</Host>
Otherwise, what is the correct solution?
Thanks to all!
I'm assuming that you have one of the instances of your app in the webapps directory, from where it's automatically deployed under its name. With the Context element, you can deploy applications from anywhere in the filesystem.
However, it's best practice to not edit server.xml with this information, but rather create individual context.xml files in conf/hostname, or just deploy to the host's webapps directory. Anything that you're configuring in server.xml requires a server restart if you want to change anything. Configuration outside of server.xml can be picked up at runtime, without restart.
Edit: Probably the takeaway of my previous answer wasn't clear:
When you use Context elements to configure: I'm proposing not to use the webapps directory for deploying your webapps. Just point to the directory where you actually deployed your webapp. This will make sure that no context is deploying your app from webapps/appname as /appname, even though your context definition points to /.
An alternative is to deploy your application with the name ROOT in webapps - this is a shortcut to actually deploy this webapp as / (careful: ALL CAPS for the directory- or file-name (ROOT.war) even on Windows if I remember correctly). But IMHO it would be clearer if you decide for either Context definition to determine the path (but deploy outside of appbase (webapps), or deployment in appbase (e.g. webapps) deployment.
Mocking around with appbase to point to some directory within webapps will make sure that you irritate anyone who is expecting that applications in webapps directory will deploy to Tomcat. That's an easy way to sink a lot of time into debugging phantom problems - don't do that.

How can I specify "Context path" on Tomcat 8 inside META-INF/context.xml in java war file?

How can I deploy mywebapp-1.0.0.war to $TOMCAT_HOME/webapps directory with context path /mywebapp using context.xml inside the war file on Tomcat 8?
I'm getting back to work with Tomcat after long time since version 5. I'm used to create META-INF/context.xml inside my war file:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mywebapp">
...
</Context>
Maven creates a war file with this name: mywebapp-1.0.0.war
But when I deploy the war file to $TOMCAT_HOME/webapps directory the context path will be http://localhost:8080/mywebapp-1.0.0 instead of http://localhost:8080/mywebapp/.
Also I see that $TOMCAT_HOME/conf/Catalina/localhost is empty, instead of having the xml file copied from the war file deployed.
I also added to $TOMCAT_HOME/conf/server.xml the deployXML="true"
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
deployXML="true">
It is not possible to put a war inside the webapp directory and set the path attribute in META-INF/context.xml file at the same time. Tomcat 8 documentation, clearly says about this attribute:
This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase.
Can't you just rename your war file to mywebapp (via Maven or else) so that Tomcat will deploy it under /mywebapp ?
Thanks for your research j2gl!
I've found out that good way how to achieve both .war file with full name with version and deployed short path is to use Tomcat manager API. For example through Tomcat7-maven-plugin.

Deploy webapp manually with Tomcat (ie autoDeploy=false, noDeployOnStartup=false)

I'm trying to deploy several web applications to tomcat 6.x, and I've turned off autoDeploy and onDeployStartup because I want to manually register these apps and map them to URLs not based on the names of their war files.
I've put the following context file in $catalina.home/conf/Catalina/localhost:
<Context path="" docBase="web-1.0-SNAPSHOT.war" debug="1">
</Context>
And I put the war file under $catalina.home/webapps, but when I startup tomcat nothing gets deployed. I don't even see any error messages about the context files I created. Or any print outs saying anything is wrong.
What's the problem? I've read the documents which outlines autodeploy a lot, but is very sketchy on details of how to do this outside of autodeploy.
So the details about how autoDeploy works, and alternative deployments is only really discussed here.:
http://wiki.apache.org/tomcat/HowTo#How_do_I_make_my_web_application_be_the_Tomcat_default_application.3F
I don't know why tomcat makes this so complicated. If you turn off autoDeploy your only option is to modify the server.xml and add your contexts there. You can't externalize the definitions of your contexts which seems convoluted way to deploy things. If I'm going to take the time to drop a XML config file I should be able to control the URL it's mounted to and the docBase. Just make it straight forward because Jetty does.
Try the following steps
Shutdown the tomcat
Copy web-1.0-SNAPSHOT.war to webapps folder.
Deploy the webapp.
Now there is a folder named web-1.0-SNAPSHOT inside webapps.
go to conf/server.xml
Add the following entries
<Context path="/abc" docBase="web-1.0-SNAPSHOT" debug="1"></Context>
The docbase doesn't have the .war extention. When web-1.0-SNAPSHOT.war is deployed there will be a directory web-1.0-SNAPSHOT inside webapps. The docbase should point to this directory.
Please make sure that Context tag is within the
<Host> </Host> tag
<Host>
<Context path="/abc" docBase="web-1.0-SNAPSHOT" debug="1"></Context>
</Host>
After editing server.xml you have to restart tomcat server to reflect the changes.
Now you can find your webapp at
localhost:8080/abc
Hope this helps
Setting deployonstartup to false tells tomcat not to deploy apps on startup. I think its enough to turn autodeploy off. so maybe try the following in the Host in server.xml: autoDeploy="false" deployOnStartup="true".
Example code
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="false" deployOnStartup="false">
<Context path="/howto-prepareexam" docBase="howto-prepareexam" debug="1"></Context>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
Then restart tomcat

Categories

Resources