• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Saloon Keeper
Posts: 22484
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

George Impagliazo
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic