Java.lang.String in default jndi context with Apache Geronimo - How?

In the servlet, I do the following:

Context context = new InitialContext(); value = (String) context.lookup("java:comp/env/propertyName"); 

In an Apache Geronimo instance (WAS CE 2.1), how do I associate a value with a propertyName key?

In Websphere AS 6, I can configure these properties to search for JNDIs on the Namespace Associations page of the management console, but for life I cannot find a way to do this in a community publication on the Internet.

+4
source share
2 answers

One possibility is to add properties to your web.xml file (in the WEB-INF directory) using one or more <env-entry> tags. For example, something like the following:

 <env-entry> <description>My string property</descriptor> <env-entry-name>propertyName</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>Your string goes here</env-entry-value> </env-entry> 

Each env-entry tag announces a new environment variable that you can access from the java:comp/env context java:comp/env .

After adding the required env-entry you can use code similar to what you already posted to access these values. Keep in mind I don't have Geronimo installed, so I don't know if there is any additional configuration that needs to be done to make this work.

+1
source

You can put your properties in a file and specify the name and location of the file URL of the resource URL in web.xml. The resource value is set in geronimo-web.xml.

Your web.xml will have the following entry:

 <resource-ref> <res-ref-name>configFileName</res-ref-name> <res-type>java.net.URL</res-type> </resource-ref> 

In geronimo-web.xml you define a value for configFileName

 <name:resource-ref> <name:ref-name>configFileName</name:ref-name> <name:url>file:///etc/myConfigFile</name:url> </name:resource-ref> 

In java, you have the following code to search for a value:

 initialContext = new InitialContext(); URL url = (URL) initialContext.lookup("java:comp/env/configFileName"); String configFileName = url.getPath(); 

Then you need to open the file and read any value.

The result of all this is that you have properties in the file on the file system. It will not be overwritten if you redeploy your application.

0
source

Source: https://habr.com/ru/post/1277224/


All Articles