Microsoft could use AOP to help enforce their Security Bloody Web Services :)
Mar 22

META-INF/context.xml

Java, Tech, Web Frameworks Add comments

It is always frustrating if you have to munge a server.xml in Tomcat, or the equivilent in other servers. At that point, your nice deployable .war file requires a README that says “oh, and make sure your server.xml is setup like this…”.

At least now we have a feature that lets us put context information in the war file. I didn’t know that Tomcat supported this until recently:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
auth="Container"
description="DB Connection"
name="jdbc/ebackbay"
type="javax.sql.DataSource"
password=""
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
validationQuery="select * from testdata;"
username="root"
url="jdbc:mysql://localhost/ebackbay"
maxActive="4"/>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>META-INF/context.xml</WatchedResource>
</Context>

21 Responses to “META-INF/context.xml”

  1. Matt Raible Says:

    This works great on Tomcat 5, but if it you want it to work on Tomcat 4 – you have to use the Manager app to deploy your WAR.

  2. Marc Logemann Says:

    I am using this since Tomcat5 is out and its really nice. I wish there would be some kind of standard so that this also works in resin or jetty.

  3. Patrick Peak Says:

    For the sake of modularity, I try to avoid ever putting anything into the server config related to the application. It just causes to many problems if you have to move apps between servers. It’s much much easier to make the .war file a standalone deployable unit.

    FYI, we typically use Resin. And using a application framework like Spring can put app details, like DB connection info into the app (where I think they belong).

  4. anand raman Says:

    Nice feature. I wasnt aware of it. However after some thought I think I would rather start using the jmx beans to deploy / undeploy applications. Atleast that way I preserve the cros deployment features of a war. Yes it definitely has an extra overhead to know of the beans which do the actual stuff.

    anand

  5. Justin Says:

    Patrick, you say that the db connection info belongs in the app? What happens when the database is moved to a different server? Just curious.

  6. Christian Menkens Says:

    Hi

    I could not configure Webapplication on my Tomcat 5.5.7 so that it worked with the context.xml in the META-INF folder. I always get the exception:

    org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class ” for connect URL ‘null’

    my context.xml looks like this:

    WEB-INF/web.xml
    META-INF/context.xml

    and my server.xml like this:

    WEB-INF/web.xml
    META-INF/context.xml

    can someone maybe help me? I would like to use this feature!

    Ohh and with the content of the context.xml in the server.xml everything worked fine!

    thx,

    Christian

  7. Scott E Says:

    Christian, I am having the same issue.

    Also, your xml posting appears blank.

    Has anyone found a solution to this problem yet?

  8. shaug Says:

    Does the context.xml you show work as-is, Dion? When I try this with Tomcat 5.0.28 with a similarly-configured context.xml, tomcat throws an exception at startup claiming I need to specify a context path. Do you have a different experience? If so, with which version of tomcat?

  9. Vivian Says:

    Hi, I also have the error
    org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class ” for connect URL ‘null’

  10. Adam Buckley Says:

    Note that the META-INF/context.xml trick only works if you are creating a WAR file. If you are deploying by pointing Tomcat to an expanded directory structure, this trick won’t work. I am using Tomcat 5.0.

  11. Lutz Müller Says:

    Christian, Scott, Vivian
    you would get this error, if you try to use a 5.0 style jndi datasource definition in tomcat 5.5 or vice versa. maybe this is the cause for your problems?

  12. The final solution Says:

    On your web.xml add the following elements

    Database Connection
    jdbc/ebackbay
    javax.sql.DataSource
    Container
    Shareable

  13. The final solution Says:

    That didn’t work

    There are xml elements to add on your application’s web.xml

    view this page: http://www.oreillynet.com/cs/user/view/cs_msg/7193

    hope this helps.

  14. Jeyakumar Says:

    hi Christian Menkens,

    Same problem came for me also

    Solution:
    i think , your are not included the mysql driver jar file inside your /WEB-INF/lib directory.

    Check the mysql driver jar is placed in /WEB-INF/lib directory. if not include the
    jar then restart the tomcat.

    for me it works fine ..

  15. Daniel Says:

    @Adam (comment 10)

    The META-INF/context.xml “trick” works fine for war files and expanded directorie structures. It is not as trick. It is a normal configuration alternative.

    I am using Tomcat-6.0.18.

  16. Nalini Says:

    Thanks Adam Buckley .. ur idea was helpful.

  17. Kiran Says:

    Hi Dion Or any one Please help me.

    I am using tomcat version 5.0.27 and trying to deploy my war file with the context information , But having trouble . War file is expanding properly but it is creating kd.xml FOLDER under C:\tomcat\Tomcat 5.0\conf\Catalina\localhost ( where kd is my war file name) , not sure why it is creating Folder with that name under conf\Catalina\localhost . The end result is exception in the log says “java.io.IOException: java.io.FileNotFoundException: C:\tomcat\Tomcat 5.0\conf\Catalina\localhost\kd.xml (Access is denied)”

    My context.xml file below

    WEB-INF/web.xml
    META-INF/context.xml

    username
    root

    password

    driverClassName
    com.mysql.jdbc.Driver

    url
    jdbc:mysql://localhost:3306/test

    Thanks for your help

  18. Kiran Says:

    Sorry guys , This was a bug in tomcat 5.0.27 . I did apply the fix and the issues was resolved

    https://issues.apache.org/bugzilla/show_bug.cgi?id=29688

    But now I am getting into another issue
    Cannot create JDBC driver of class ” for connect URL ‘null’
    I copied the required jar to Lib folder , But still getting this error , Can you please help me . am I missing any thing?

    Regards

  19. Kiran Says:

    Hi Dion ,

    I am able to resolve this issue finally , problem is with context path.

    Regards

  20. Steve Says:

    I’m using Tomcat 6 and it works. I added the JDBC details to a copy of the default context.xml and put it in the applications META-INF folder. *** Note. you must put it in the WAR file editing the expanded folder does not work ***

  21. Chris Says:

    Does anyone know how to keep the context.xml file from being overwritten each time a new WAR file is deployed? Here’s the situation: our context.xml file contained in the WAR file is meant to be configured AFTER it is deployed. For example, our client services team will fill in values like database connection strings and such. However, when the WAR file is redeployed these values are overwritten, and they will have to fill in those values again. I’d like to know if anyone has a solution to this.

    Thanks!

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'ajax'