Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

how to configure a JNDI URL resource

Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to replicate the Websphere's URL Resource provider functionality in Tomcat, but haven't been really successful. So what I would like to do is access a properties file from a URL. This provides a convenient approach to modifying prop files as apposed to residing within WAR file. Anyway back to my question, I have taken a stab at this and below is how my meta-inf\context.xml, web.xml, and jndi code snippet looks.



<?xml version="1.0" encoding="UTF-8"?><Context> <Resource name="url/wfresource" auth="Container" type="" url="http://localhost:8080/ibi_html/"/></Context>




<resource-env-ref> <description>Object factory for MyBean instances.</description> <resource-env-ref-name>url/wfresource</resource-env-ref-name> <resource-env-ref-type></resource-env-ref-type></resource-env-ref>


JNDI Lookup code snippet


//Get a handle to the JNDI environment naming contextContext env = (Context)new InitialContext().lookup("java:comp/env");//Get a single destURL = ("url/wfresource");


Its throwing an exception.

My question is if URL resource configuration is possible in Tomcat 5.5, and if so how.

Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rohit,

We are doing it that way where I work, but it is a little different than how you describe.

One way is to use the Tomcat admin page and create a new datasource. That takes care of part of the picture. I had to do quite a bit of hacking to get it to work from beginning to end. I'll describe the steps, and hopefully you can retrofit them for your use.

Part of the configuration is in a properties file:
Say we want to setup a MySQL connection. In the file, add a line like:


To access this value, do something like this:

After loading the properties file, you can call the getJndiName method passing dbName as "mysql". This method is useful if you are setting up several database resources. Of course, you'll need to add them to the properties file as well.

To establish a connection, create a method similar to this:

Now, you're ready to make the query code:

The fun part is getting the connection to work. I ended up using the application context. So in $CATALINA_HOME/conf/Catalina/localhost, I have my file. In your case it would be something like myapp.xml. Create the file as follows:

Important: MAKE A COPY OF THIS FILE. If you redeploy your web app, it's very possible Tomcat will nuke it.

There's something the Tomcat admin util is *supposed* to configure. With 5.5, I had to beat it into submission with a ball peen hammer to get it to work. If you use it, in the <GlobalNamingResources> section, look for:

It should reflect the settings you added through the admin page. You can use this as a reference if you prefer not to use it though.

Hope this helps...

-- Nothing is impossible if I'mPossible
Message for you sir! I think it is a tiny ad:
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic