0

How can i set simple string value in configuration of tomcat and then read in java application?

context.xml

<ResourceLink name="global/test" global="testing" type="java.lang.String" />

server.xml

<Enviroment name="testing" value="myUser" type="java.lang.String"/>

web.xml in application

<resource-env-ref>
    <resource-env-ref-name>global/test</resource-env-ref-name>
    <resource-env-ref-type>java.lang.String</resource-env-ref-type>
</resource-env-ref>

in my java application

public String getValue(){
    return new JndiDataSourceLookup().getDataSource("global/test").toString();
}

When i Run tomcat, i see these errors...

org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'global/test'; nested exception is javax.naming.NameNotFoundException: Name [global/test] is not bound in this Context. Unable to find [global].
javax.naming.NameNotFoundException: Name [global/test] is not bound in this Context. Unable to find [global].
fubu
  • 3
  • 1
  • 5

2 Answers2

4

In your web.xml use,

<env-entry>
<description>Sample env entry</description>
<env-entry-name>isConnected</env-entry-name>
<env-entry-type>java.lang.Boolean</env-entry-type><!--order matters -->
<env-entry-value>true</env-entry-value>
</env-entry>

In code,

try {
 Context initCxt =  new InitialContext();
 Boolean isConn =  (Boolean)initCxt.lookup("java:comp/env/isConnected");
 System.out.println(isConn.toString());
 // one could use relative names into the sub-context
 Context envContext = (Context) initCxt.lookup("java:comp/env");
 Boolean isConn2 = (Boolean)envContext.lookup("isConnected");
 System.out.println(isConn2.toString());
} catch (NamingException e) {
 e.printStackTrace();
}

Have a look here Naming service tutorial to get a good understanding of InitialContext and JNDI.

John Eipe
  • 10,922
  • 24
  • 72
  • 114
  • after this line Boolean isConn = (Boolean)initCxt.lookup("java:comp/env/isConnected"); is exception javax.naming.NoInitialContextException: Need to specify class name in enviroment or system property ,or as an aplet parameter, or in an application resource file: java.naming.factory.initial – fubu Oct 05 '15 at 09:21
  • Try http://stackoverflow.com/questions/28995272/noinitialcontextexception-error-tomcat – John Eipe Oct 05 '15 at 09:58
  • which version of tomcat are you using? – John Eipe Oct 05 '15 at 10:02
  • where you able to fix it? – John Eipe Oct 05 '15 at 10:15
  • I ran the exact same code on tomcat 8 a moment ago and it got working without breaking a sweat. – John Eipe Oct 05 '15 at 12:05
  • I created simple web application for testing with one java class with main function. I used only your code and i got same exception. I dont understand, i need some library or something like that ? or some problem with intelij idea? – fubu Oct 07 '15 at 11:26
  • Now I get it. You cannot do this in a main function. The code should execute in the server's context – John Eipe Oct 07 '15 at 14:30
  • Simplest way to do that is by putting the java code in a servlet. – John Eipe Oct 07 '15 at 14:30
  • Yes. In post or get. Read though http://www.cs-repository.info/2014/04/jndi-tutorial-for-java-programmers.html?m=1#NamingService to get a good understanding – John Eipe Oct 08 '15 at 09:24
  • Thanks, it's work. But, i need to read string value out of PUT,GET...methods, some option? – fubu Oct 12 '15 at 11:45
  • Please pick a book or tutorial on J2EE basics. It will cover all these details – John Eipe Oct 12 '15 at 15:08
1

I don't know what's inside JndiDataSourceLookup().getDataSource("global/test") but by the name of it, it should return a DataSoruce not a string.

If your lookup is local, simply do

Context ctx = new InitialContext();
String s = (String) ctx.lookup("global/test");

or if you are in a javaee container,

@Resource(name="global/test")
String testString;

and finally in your ejb-jar.xml

<env-entry>
    <description>The name was explicitly set in the annotation so the classname prefix isn't required</description>
    <env-entry-name>global/test</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>StringValue</env-entry-value>
</env-entry>

Refer this link: http://tomee.apache.org/examples-trunk/injection-of-env-entry/README.html

Your configuration of context.xml, server.xml, and web.xml aren't gonna work.

ares
  • 4,283
  • 6
  • 32
  • 63
  • Since you're using tomcat, and have a web application, you can add the `` content in your `web.xml` and remove `ejb-jar.xml`. – ares Oct 05 '15 at 10:23
  • I added to web.xml, but this line String s = (String) ctx.lookup("global/test"); give exception again javax.naming.NoInitialContextException: Need to specify class name in enviroment or system property ,or as an aplet parameter, or in an application resource file: java.naming.factory.initial – fubu Oct 05 '15 at 10:29
  • javax.naming.NoInitialContextException: Need to specify class name in enviroment or system property ,or as an aplet parameter, or in an application resource file: java.naming.factory.initial – fubu Oct 05 '15 at 10:33