jQuery in Action, 3rd edition
The moose likes Tomcat and the fly likes Tomcat 7:  Can I configure it  so that I don't need to use “java:/comp/env” in the code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat 7:  Can I configure it  so that I don Watch "Tomcat 7:  Can I configure it  so that I don New topic

Tomcat 7: Can I configure it so that I don't need to use “java:/comp/env” in the code?

George Impagliazo
Ranch Hand

Joined: Sep 25, 2010
Posts: 41
I am new to setting up JNDI resources and setting up JNDI resources in Tomcat.

I inherited a servlet application. It runs on a test server via WebLogic. The servlet application accesses its database resource in the following way:

When I tried that in a test JSP it doesn't work. I get

However I was able to make the test JSP work if I altered the test JSP code thus:

I have this entry in TOMCAT_HOME/conf/context.html ( I'm just using it as a dev environment on my box )

And I have this in my TOMCAT_HOME/conf/web.xml:

How can I change the way I have Tomcat 7 set up to allow the existing servlet application to access its database as "myDatabaseName"? I don't have the option of altering the code in the servlet application or altering the way WebLogic is set up on the test server.

I need to alter the way my copy of Tomcat 7 is set up on my computer ( for a dev environment ) to allow the servlet application to access its database in the style of the first chunk of quoted code at the top of this post.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Actually, it's WebLogic that's at fault here:

Some application servers let you look up resources by their global JNDI name. In such case, their lookup name should not contain java:comp/env. For example, java:/defaultDS in jboss. In JavaEE SDK 5/Glassfish/Sun Java System Application Server 9, I can also directly look up jdbc/__default (the default datasource) without configuring it in any descriptors. Note that this style of lookup is not portable. It ties your apps to specifc runtime server environment, and should really be avoided.

(taken from http://javahowto.blogspot.com/2006/07/when-and-where-not-to-use-javacompenv.html)

The official scoop on the standard JNDI URLs for J2EE is outlined here: http://download.oracle.com/javase/jndi/tutorial/beyond/misc/policy.html

The scheme part of the URL ("java:") is required in order to distinguish between the differing services that JNDI affords, since JNDI is a general-purpose facility that can work with many different system. Thus, "java:comp/env/myproperty" is going to be routed to a completely different subsystem (the J2EE component directory service) than a URL such as "ldap:cn=myproperty,dc=mousetech,dc=com". Which is how you'd lookup something on an LDAP server such as Microsoft's Active Directory.

The base level ("comp") stands for "component", and indicates a particular branch of the "java:" directory domain. "env", of course, is "environment". In effect, JNDI java:comp/env is J2EE's equivalent of environment variables. Which is one reason why I tend to advise people not to use "real" environment variables in J2EE apps. More particularly, OS environment variables are common to every app in the JVM, whereas a separate JNDI directory tree exists for each webapp, allowing you to customize the environment on a per-app basis.

You can minimize the incompatibility between your WebLogic environment and Tomcat by breaking the resource lookup into 2 parts, 1 to get the java:comp/env directory and the other to find the resource within it. But I'm fairly sure I used to use java:comp/env even in WebLogic.

An IDE is no substitute for an Intelligent Developer.
George Impagliazo
Ranch Hand

Joined: Sep 25, 2010
Posts: 41

So basically it is non-standard to use the servlet container/webserver/appserver prepend "java:comp/env", but WebLogic and some others apps allow it and this makes your webapp less portable.

Unfortunately, I don't have the power to change that part of the database code or the way WebLogic is set up.

Fortunately, Tomcat has only been for my dev environment on my computer. I guess at this point I will have to abandon Tomcat and learn how to set WebLogic to test my JSPS/servlets on my computer.

Thanks for the information
I agree. Here's the link: http://aspose.com/file-tools
subject: Tomcat 7: Can I configure it so that I don't need to use “java:/comp/env” in the code?
It's not a secret anymore!