2 Replies Latest reply on May 11, 2006 12:45 PM by Laird Nelson

    Creating a JNDI binding from a servlet?

    Laird Nelson Apprentice

      What's the recipe for creating a JNDI binding from a servlet? Obviously I know the JNDI APIs. However, a new InitialContext() call does not find the local JNDI tree. I'd like to be able to do exactly this:

      final InitialContext c = new InitialContext();
      c.rebind(someName, thingToBind);

      ...but that doesn't work. The rebind() call cannot find the JNDI tree, which doesn't particularly surprise me in the abstract, since I certainly haven't supplied any JNDI properties either (a) on disk or (b) in the InitialContext constructor. But I feel like I shouldn't have to--I'm running in a servlet on the server, after all, so shouldn't a new InitialContext() call in such an environment somehow know that the local JNDI tree is the one I want?

      (I filed this under Configuration and Deployment because that's effectively the stage during which I'm doing this; if this is an incorrect location, my apologies.)

      Thanks,
      Laird

        • 1. Re: Creating a JNDI binding from a servlet?
          Peter Johnson Master

          The following works for me:

          Hashtable env = new Hashtable();
          env.put(Context.INITIAL_CONTEXT_FACTORY,
           "org.jnp.interfaces.NamingContextFactory");
          env.put(Context.PROVIDER_URL, "localhost:1099");
          Context ctx = new InitialContext(env);


          I do agree with your comment that the server should give you the local JNDI tree is you don't give any properties.

          • 2. Re: Creating a JNDI binding from a servlet?
            Laird Nelson Apprentice

            Thanks, Peter; I know I can get away with that. You can actually get away with quite a bit less:

            final InitialContext ic = new InitialContext(new Hashtable() { { this.put(Context.PROVIDER_URL, "localhost"); } });
            

            ...if everything has been set up for the defaults.

            It looks like the answer may very well be no, if you are a servlet trying to bind something then you are a client like any other client, and you must therefore somehow specify what JNDI server to attach to. This makes a certain amount of sense; I was just hoping for, say, a jndi.properties that got put somewhere when a given EAR/WAR/whatever (or when the JBossNS system itself gets deployed) saying what port it's on, what host it thinks it's on, etc.).

            Thanks again,
            Laird