How to set context path in context.xml - java

I have to deploy my app.war file in tomcat 7. The .war file name is followed by its version no.
Here I need to set up a context path, so that the actual url will contain only the app name(without version no).
My requirement is that, there should be no edit in server.xml.
My context.xml is as follows.
<?xml version='1.0' encoding='utf-8'?>
<Context path="/app" docBase="app-1.0" debug="0" reloadable="true">
<!-- Defines links to JNDI Data Sources -->
<!-- Thus the server determines database connection. -->
<ResourceLink
name="..."
global="..."
auth="Container" type="javax.sql.DataSource"/>
.....
.....
</Context>
The context.xml is placed inside the war at /META-INF folder.
Can anyone tell me where am i wrong.

All the elements are in the docs :
http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Naming
For your use case, you could do try :
change your version number format (from app-1.0.0.war to app##1.0.0.war
for instance)
place your war in another folder and create a app.xml
in $catalina.base/conf/Catalina/ which contains : <Context path="/app" docBase="/path/to/app-1.0" debug="0" reloadable="true">
avoid having your war with version number

Had a similar problem and it took me a long time to find the solution. It's on the tomcat site but it's difficult to find. This is what I did.
Your war file will be deployed to a folder under %CATALINA_BASE%. I put mine in a folder called deploy. (%CATALINA_BASE%/deploy)
You'll create an XML file with the path to your war file above and place it in %CATALINA_BASE%/conf/Catalina/localhost. The name of the xml file will become your context root. If the name of your war file is app1.2.war and you want your context root to be /app you're create app.xml:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="C:\tomcat7\Servers\server-app\deploy\app1.2" reloadable="false"/>
(My deployment is on Windows, you'll obviously have to adjust for a different OS.)
Also, let's for whatever reason say you want your context root to be /foo/Bar/app, change the name of your xml to foo#Bar#app.xml.

Related

META-INF/context.xml causing problems in my Java Webapp

I'm deploying a simple Java 7 (I used Maven for project set-up, dependencies, etc) web app to Tomcat 8 and I have a META-INF/context.xml that I need to specify my database resource:
project/src/main/resources/META-INF/context.xml
<xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/javatest"/>
</Context>
When I remove this META-INF/context.xml file from the project, I am able to access my jsps but, of course, they return errors since my datasource is missing. However, when I include META-INF/context.xml back to the project, all resources that I try to access give me a 404. Why does it behave this way?
For reference, I am trying how to use a JNDI data source by following this guide. I did all the steps necessary in that project
Are there any exceptions when the server is starting?
If the jdbc driver is not bundled in WEB-INF/lib and not in CATALINA_BASE/lib then it can't find the class. That would most likely result in a startup failure.
Check catalina.out (if you are on unix) or CATALINA_BASE/logs/localhost/catalina.date.log
Edit
Just noticed you have src/main/resources/META-INF
Try src/main/webapp/META-INF/context.xml ...
Do not remove the META-INF/context.xml because it is the default configuration for the project! Also do not enter production-password into the default META-INF/context.xml!
Use copyXML="true" instead! On first deployment to tomcat the META-INF/context.xml is copied permanently into tomcat/conf/Catalina!
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.
After first deployment update tomcat/conf/Catalina/webappname.xml to productive database informations.
Any redeployment will keep using the tomcat/conf/Catalina/webappname.xml.

Deploy multiple webapps with application specific config in Tomcat from Eclipse

I have a Tomcat 7 Server in Eclipse with two webapps a and b and need to find out how to have application specific configuration files for tomcat that works with Eclipse.
Now I want for each webapp a custom config file, like described here: Apache Tomcat: multiple webapps. So I created a <TomcatDirectory>/conf/Catalina/localhost folder and put the a.xml and b.xml there. This works fine when I run Tomcat from command line.
But where have I to put the application specific configuration files a.xml and b.xml when I run Tomcat from within eclipse?
I already tried to put the files in Eclipse in <EclipseWorkspace>\Servers\<ServerName>\conf\Catalina\localhost\ but WTP does not copy this files to <EclipseWorkspace>\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost
Leave them in <TomcatDirectory>/conf/Catalina/localhost
Server > Open > Server Locations
Change from:
Use workspace metadata
To:
Use Tomcat instalation
Now a.xml and b.xml from <TomcatDirectory>/conf/Catalina/localhost will be used.
If it's for development's purpose, you can specify a folder into the "docBase" attribute of context file like this :
<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true" docBase="C:/devel/MyApp/web/target/MyApp-web" path="/MyApp" reloadable="true">
<Resource
... />
</Context>
By adding these context files directly into /conf/Catalina/localhost, everything is configured and should work like you want but you'll have to stop the tomcat server every time you'll want to clean and build your project.
What you are looking for is to have context.xml files for two different applications.create context.xml file under web application's (a and b) META-INF folder and you should be able to run both the application with their own context.xml file. In eclipse you should be able to create META-INF folder for your respective web applications in parallel to WEB-INF directory

How to change the default value of context.xml in Tomcat?

I am trying to use the crossContext feature in Tomcat so that I need to set <Context crossContext="true"> in the context.xml file located in mypath\apache-tomcat-7.0.41\conf folder.
However I find that every time I restart tomcat it will restore to its original value then I have to modify that again. I want to know if there is a way to avoid this so that I do not need to modify the context.xml file every time I restart Tomcat?
Thanks in advance!
<Context crossContext="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Manager pathname="" />
</Context>
above is the code I am using...
According the the answer in this question [link] Tomcat context.xml files, is there a hiearchy?
I figured out the way to solve this problem. Just create the context.xml file in the location mypath\apache-tomcat-7.0.41\conf\Catalina\localhost and add in <Context crossContext="true"> </Context> then you are done.
You need to undeploy your web application and then re-deploy it. Tomcat is re-using the context.xml file you provided on initial deployment, which you can find in CATALINA_BASE/conf/[engine]/[host]/[context].xml. The changes you are making to your own context.xml are probably being completely ignored.

tomcat7 doesn't read context xml file

I would like to specify context for db in xml file.
<Context path="/db3" docBase="C:/my/workspace/db3/">
<Resource name="jdbc/ksidb" auth="Container"
type="javax.sql.DataSource"
description="Books"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/ksidb"
username="root"
password="root"
maxActive="20" />
</Context>
I've read that I should copy that file to /webapps tomcat catalogoue. I did this but tomcat7 doesn't read the file. Do you know why? What to do? Thx.
What you read is wrong. I'd question other advice from that source if it told you something so completely false. Per the Tomcat docs, your options for placing the context configuration are as follows:
In an individual file at /META-INF/context.xml inside the application files. Optionally (based on the Host's copyXML attribute) this may be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to application's base file name plus a ".xml" extension.
In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context path and version will be derived from the base name of the file (the file name less the .xml extension). This file will always take precedence over any context.xml file packaged in the web application's META-INF directory.
Inside a Host element in the main conf/server.xml.
I highly recommend you visit the linked docs to learn more about the correct way to configure Tomcat.

What can cause a webapp I deployed to Tomcat to be accessible via the root path?

I copy-paste-deployed a webapp to a local installation of Tomcat 6 (I place "my-app" folder under "webapps"). I expected my app to be accessible via http://localhost:8080/my-app/.
Instead, I find that it is accessible directly via the root path (http://localhost:8080/).
What configuration can be the cause of this?
This is my context.xml (comments stripped)
<?xml version='1.0' encoding='utf-8'?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
If you're using IntelliJ, the web path to your application is specified in Edit Configuration -> Deployment tab. You can click on an artifact you're deploying and change its web path in Application context dropdown (which is / by default)
Depends what you copied and pasted but take at look at the ROOT.XML file in Tomcat and see whether that's configured to serve the application instead.
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

Categories