I have tomcat's context.xml file with references to the context.properties file
<Context sessionCookiePath="/" path="/">
<Resource name="jdbc/dbwriter" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
type="javax.sql.DataSource"
url="jdbc:mysql://${context.ip}:3306/test" username="${user}"
password="${pass}" />
</Context>
The context.properties file looks like:
context.ip=localhost
user=admin
pass=admin
But it seems that tomcat does not load the properties file and tries to resolve jdbc:mysql://${context.ip}:3306. What am I doing wrong?
For build I am using maven. Maybe it should fill up the property placeholders?
EDIT: the context.properties file is located in side WEB-INF/classes folder, and context.xml is located in the META-INF folder inside the WAR file.
There is little point shipping your context.properties file as part of the web application. You might as well have the build system do the property replacement for you.
If you want to keep the property definitions, Tomcat will pick up those properties if you add them to the catalina.properties file located in $CATALINA_BASE/conf
Related
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 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.
I'm working on a Java web application in Eclipse and deploying to an instance of Tomcat that is run by Eclipse. I'm trying to get this application to talk to a database on another host via a JNDI Resource element.
The context.xml file included in the application attempts to connect to a MySQL server running on localhost, like so:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="log4j.configuration"
value="/path/to/installed/log4j.properties"
type="java.lang.String" />
<Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbname" maxActive="8"
validationQuery="SELECT 1" testOnBorrow="true" />
<Environment name="mykey" value="installed" type="java.lang.String" />
</Context>
This configuration works when I install it on the actual host. That is, it reads the environment values correctly and connects to the database.
I can also install this on a separate host and edit /etc/tomcat6/conf/Catalina/localhost/myaplication.xml to change the JDBC URL, like so:
...
<Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://otherhost:3306/dbname" maxActive="8"
validationQuery="SELECT 1" testOnBorrow="true" />
...
This configuration also works. In other words, I'm pretty confident that my application is using the JNDI correctly.
So, now I want to deploy this to Tomcat on my desktop in Eclipse. I've added the project to Tomcat in the standard way, and I've edited the context.xml file for the server (i.e., the one that Eclipse shows under my Tomcat server in the "Servers" project) like so:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="log4j.configuration"
value=C:\workspace\myapplication\src\main\resources\conf\log_to_tomcat_console_log4j.properties"
type="java.lang.String" />
<Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://otherhost:3306/dbname" maxActive="8"
validationQuery="SELECT 1" testOnBorrow="true" />
<Environment name="mykey" value="desktop" type="java.lang.String" />
</Context>
When I then restart Tomcat in Eclipse, log shows me that the path to the Log4J file is being read correctly (because it starts logging to the Tomcat console) and I even see it spit out the correct "mykey" value. However, initialisation fails when it tries to read from the configDB.
At first, I thought this was a connectivity issue, but after ruling that out, I restarted Tomcat in the debugger and inspected the DataSource read from JNDI; its URL points to localhost, meaning it's using the one in the application's context.xml file, not Tomcat's!
I've tried several ways to get this working, including putting this information in Tomcat's server.xml file, but nothing seems to work. Or, more accurately, it sort of kind of works because it's reading the Environment values but not the Resource.
What am I doing wrong here? How do I get Eclipse's Tomcat server to override this Resource?
The basic problem here is, that the context.xml you edit in eclipse corresponds to the main context.xml in the conf directory of tomcat. And then the preferences in the application context file takes precedence. As you already described within normal deployment lifecycle of tomcat this is perfectly fine, because you just edit the application settings.
You could do the same with the context file created by eclipse (which is located somewhere at .metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf/Catalina/localhost) but it will be overwritten as soon as you change something in the original context file (META-INF/context.xml).
So actually you would need to make the changes there, but this is normally out of question because this file must be deployed without custom changes.
But here is a workaround: You can make eclipse use another META-INF/context.xml by abusing the deployment assembly. Make a new META-INF directory somewhere else and place a context.xml file with your setting there. Then in the project properties of your eclipse project at Deployment Assembly add an additional mapping from your new path/to/META-INF to META-INF.
Then eclipse will overwrite the original context.xml with your custom one and tomcat should take your settings.
I'm developing java web application using tomcat 7
I want to have context.xml file outside of my war with custom name. I have database resource defined in this file.
According to tomcat documentation, I put context.xml.default file in CATALINA_HOME/conf/[enginename]/[hostname]/ directrory and I could get database configuration.
Now, I want to rename context.xml.default and put custom file name,which matches my contextpath. If I do so, I can't get database configuration anymore and my appliction throws Exception:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
Here is the content o my context.xml.default file:
<Context
crossContext="true"
reloadable="true" >
<Resource
name="jdbc/myDS"
auth="Container"
driverClassName="oracle.jdbc.driver.OracleDriver"
factory="com...encription.util.EncryptedDataSourceFactory"
logAbandoned="true"
maxActive="30"
maxIdle="10"
maxWait="1000"
password="encryptedPass"
removeAbandoned="true"
removeAbandonedTimeout="60"
type="javax.sql.DataSource"
url="jdbc:oracle:thin:#myIP:myPort:FRONT"
username="username" />
The configuration I used was correct but the problem was that eclipse was using {catalina_base}/wtpwebapps directory to deploy web applications.
I changed wtpwebapps folder with webapps in server configuration in eclipse and now everything works correctly.
The error might be a result of a missing JDBC driver. Try putting the Oracle driver into the %TOMCAT_HOME%/common/lib directory.
Another possibility is, that you are missing a web-application specific context.xml (named <NAME_OF_WEBAPP>.xml) in conf/catalina/localhost.
I have a web application and use tomcat-5.5.27, eclipse 3.4 on open SUSE. I use it from eclipse (deploy, run, debug, etc...)
I have to set some JNDI resources (datasources) that Tomcat will expose them.
These resources are defined now in \META-INF\context.xml. This works fine.
But, when I move this file (context.xml) in tomcat-5.5.27/conf/context.xml or
tomcat-5.5.27/conf/Catalina/localhost/myapp.xml, it does not load the resources defined in the file.
What can be the problem?
My context.xml:
<Context path="/myapp" docBase="/myapp" debug="5" reloadable="true" crossContext="true">
<Resource name="jndi_name" auth="Container"
type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="COM.ibm.db2.jdbc.app.DB2Driver"
url="jdbc:path_to_db"
username="username"
password="password"
validationQuery="SELECT 1 FROM SYSIBM.SYSDUMMY1"
defaultTransactionIsolation="READ_COMMITTED"
removeAbandoned="true" logAbandoned="true" >
</Context>
If you're using the web tools project feature in Eclipse to manager your Tomcat instance you need to know that it does change Tomcat base directories.
Try using a separate Tomcat install and see if that helps.
This could be a classpath issue when tomcat comes to load the JDBC driver, (ie. before it loads your webapp).
Do you have the JAR file for the DB2 driver in tomcat-5.5.27/common/lib ? It must be there for
the class loader to find it, and not in your webapp's WEB-INF/lib directory.
where is your webapp stored ? It's own folder or under webapps ? check if your docBase is valid, if your app is under webapps I don't think you need the "/"
Any relevant info from the Tomcat logs ?
Open the tomcat server in the server view, modify anything like the timeout, save it and your context will be recognized.
What do the tomcat logs show when you start it up?
That tag is not closed, check if you copied it to question correctly.