Problem with Tomcat9 Context's docBase attribute - java

I am new to Tomcat deployment.
I found in Tomcat's document and it says this about <Context> tag's docBase attribute:
The value of this field must not be set unless the Context element is defined in server.xml or the docBase is not located under the Host's appBase.
Here is my problem:
I create a <Host> tag inside server.xml and set appBase="C:/xxx/"
create a xml file named ROOT.xml inside C:/xxx/ and add <Context docBase="../yyy" />
put index.html inside C:/yyy/
Why is it doesn't work when I try to browser localhost:8080/ ?
Doesn't it supposed to show the index.html inside C:/yyy ?

Summary
When you add appBase="C:/xxx" to server.xml, that means all your web appplications will be found in that directory. At that point you cannot then have a specific web app which defines its own redirection to another directory (a custom docBase) from within its own web application location. At that point, its docBase location has already been set to be the appBase directory.
A Different Approach
The ROOT.xml file that you created can be used to point to a location which is different from the appBase - but you cannot place that file inside the appBase directory (as noted above).
Instead you have to place it in $CATALINA_BASE/conf/[enginename]/[hostname]/ as described in this documentation page.
So, for me, that would be:
C:\tomcat_9_test\conf\Catalina\localhost\ROOT.xml
And its contents would simply be:
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:/yyy" />
Because this is outside of the docBase, this specific application can have a different location from all the other web applications in docBase.
Now, your C:\yyyy\index.html file will be served at http://localhost:8080/, as expected.
A Note on context.xml
What if you do not use this ROOT.xml file at all, but instead you create a ROOT folder in C:\xxx and then create a META-INF folder there, containing a context.xml file?
You could define <Context docBase="../yyy" /> in there. But it would be ignored (again, because the docBase is already set).

Related

How to set up Tomcat to use the config file in conf/Catalina/localhost?

so my question is how can I set up a Tomcat Server to use this configuration file conf/Catalina/localhost/MyApp.xml?
It works like a charm if my application is named like this: MyApp.war so tomcat will extract the archive to MyApp.
But I want to use a name with the version inside like MyApp##1.0-SNAPSHOT.war.
Is it possible to configure the tomcat so it will use the MyApp.xml anyway?
You can set path and docBase attributes of Context element as described in the docs.
Create a directory outside $CATALINA_HOME/webapps and place war files there, let's say /opt/tomcat/wars/. Then set docBaseattribute to the full path to your war. Make user tomcat user has read permissions on that directory. If war is inside webapps directory you could end up with a double deployment depending on deployOnStartup and autoDeploy attributes of Host element.
<Context path="/MyApp" docBase="/opt/tomcat/wars/MyApp##1.0-SNAPSHOT.war">
...
</Context>
You can try also naming the xml the same as the war file.

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.

Relative path in Context.xml

Is there a way to set a relative path to the docBase attribute in the context.xml of a web application, so it is outside of the appBase directory of the tomcat server instance?
I would like to be able to share the context configuration between computers and have the app living in a directory, not a war file. That way i can compile the classes directly into that directory (in my project development directory) and have tomcat use these classes without any copying/packaging needed.
I am using the tomcat 8.0.0-RC5.
My directory Layout is:
/home/david/projects/frontend/web-content <-- the static html files
/home/david/projects/frontend/web-content/WEB-INF <-- the WEB-INF with the web.xml
/home/david/projects/tomcat <-- tomcat base directory
/home/david/projects/tomcat/Catalina/localhost <-- holds the frontend.xml context configuration
I have tried
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/frontend" docBase="../../frontend/web-content">
</Context>
but that did not work. The whole path before /web-content seems to be ignored. The log says:
The main resource set specified [/home/david/projects/tomcat/webapps/web-content] is not valid
The Tomcat 8 documentation for the context container says:
You may specify an absolute pathname for this directory or WAR file, or a pathname that is relative to the appBase directory of the owning Host.
Does relative here mean a strict subdirectory of appBase (no .. allowed)?
Setting an absolute path works without problems. The configuration
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/frontend" docBase="/home/david/projects/frontend/web-content">
</Context>
works, but it is specific to my computer. So I cannot share the context configuration without modification anymore.
I could create a symbolic link inside the appBase directory of the tomcat server and let it point to the web-content folder of my application. This would work, but I would have different configurations (symbolic links) on linux and windows machines.
Just taking only the last name of relative path names and ignoring the first parts
is most likly a bug in tomcat. Relative path names should work or must throw errors.
But ignoring parts is bad.
A workaround could be using an absolute path name like this:
<Context docBase="${catalina.home}/../www.war"
I just reading the changelog of Tomcat 8.0.15.
The bug should be fixed(!):
"Correctly handle relative values for the docBase attribute of a Context."

tomcat war deployment

I'm deploying a war file in tomcat name being "myapps.war". I've made an entry in server.xml for docbase to be "/callmyapp". But after deployment two folders are getting created in the tomcat webapps directory -
myapps
callmyapp
and now I can access my war using two urls one starting with /myapps and another /callmyapp.
I want only /callmyapp as context root to this war and do not want to respond to requests /myapps. Any help?
Thanks,
Charu
rename myapps.war to callmyapp.war. tomcat automatically unpacks the war into the directory of same name. Setting a different context then also creates a second directory.
Or you can define a context.xml and put it in your /webapps directory :
<Context path="/callmyapp" docBase="webapps/myapps.war" />
Have you tried the rename the app.war ? I think the root is inferred from the filename.

customize web application root context in apache

I have web application abc.war and I want to deploy it on Apache Tomcat.
The problem is that, by default, the path to this application is http://<server-name>/abc
but I want to access it as http://<server-name>/xyz.
I put into WAR's META-INF folder the file context.xml that is :
<Context path="/xyz" docBase="abc" override="true" />
The application WAR abc.war is located under %CATALINA_HOME%\webapps and it is extracted to %CATALINA_HOME%\webapps\abc folder.
Also, while deployment, the file context.xml from abc/META-INF is copied to %CATALINA_HOME%\conf\Catalina\localhost as abc.xml
It seems that this should work, but I still can't access my application through http://<server-name>/xyz, but only through http://<server-name>/abc
In addition, I still see in apache log the following line while deployment of abc.war :
context path = /abc
Could anybody, please, help while this doesn't work, or tell if there is any way of deploying of web application on apache such that application could be accessed by customized path (that does not relate to war-file name) ?
Thanks in advance.
Take a look at the docs:
The context path of this web application, which is matched against the beginning of each request URI to select the appropriate web application for processing. All of the context paths within a particular Host must be unique. If you specify a context path of an empty string (""), you are defining the default web application for this Host, which will process all requests not assigned to other Contexts.
The value of this field must not be set except when statically defining a Context in server.xml, as it will be inferred from the filenames used for either the .xml context file or the docBase.

Categories

Resources