Pavel Leonov
Pavel Leonov

Reputation: 346

"lookup-name" of JNDI entry in tomcat

I have few applications that use JNDI properties configured in its web.xml:

<env-entry>
    <env-entry-name>application1/username</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>admin3</env-entry-value>
    <lookup-name>username</lookup-name>
</env-entry>

I cannot get the entry by lookup name, following code returns null:

String jndiValue = jndiValue = ((Context) new InitialContext().lookup("java:comp/env")).lookup("username").toString();

It seems like Tomcat do not support this attribute, is it right?

Upvotes: 0

Views: 2214

Answers (1)

user207421
user207421

Reputation: 311039

application1/username

So the partial name is application1/username.

String jndiValue = jndiValue = ((Context) new InitialContext().lookup("java:comp/env")).lookup("username").toString();

So you are looking up the partial name username.

You can simplify it as well. You don't need the nested Context, or the two Context leaks, or the toString() part either:

Context initialContext = new InitialContext();
String jndiValue = jndiValue = initialContext.lookup("java:comp/env/application1/username");
initialContext.close();

Upvotes: 1

Related Questions