You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christian Spatz <ch...@web.de> on 2007/01/25 09:24:17 UTC

write to Tomcat JNDI

Hi,

I need to bind objects using JNDI in my servlet running on apache tomcat
. Since I'm new to the JNDI topic, I did a lot of reading the last few
days and got mixed up a little. The tomcat documentation says that the
initialContext is read-only, but then I found  the hint to a solution
in  the mailing list archives
(http://mail-archives.apache.org/mod_mbox/tomcat-users/200409.mbox/%3C415DC828.3090908@fedex.com%3E#archives)
I call the initialContext Constructor passing an environment to it and
omit the "java" prefix when creating the subcontext - and it works:

Hashtable env = new Hashtable();
           
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.naming.java.javaURLContextFactory");
            env.put(Context.URL_PKG_PREFIXES,"org.apache.naming");
            Context initContext = new InitialContext(env);
            Context myContext = initContext.createSubcontext("wtl");
            Context jdbcContext = myContext.createSubcontext("jdbc");
            String test = new String("Hallo!");
            jdbcContext.bind("test", test);
            out.println("Object successfully bound!");
            String message = (String) jdbcContext.lookup("test");
            out.println("Message: " + message);

But why does this work? From what I've read I thought the prefix was
necessary to associate the url with the context factory. I thought of
this prefix as something specifying the protocol like http in web urls,
but then this shouldn't work - and why can I write to the context anyway
using this method?

Does this work with other containers as well or just with tomcat?

Thanks,
Max

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org