R – Access environmental parameters in web.xml from Web Service application

environment-variablesweb servicesweb.xml

I have to build two web service applications to be deployed to different servers (WebSphere 6.1). One of them is “linked” to another by calling its services, so the first web service application should be aware of the endpoint URL of the second one.

Those applications are working in different environments: Dev, Test and Production.
I have decided to store that URL as an environmental parameter in web.xml file of the first application to have some flexibility with moving them from one development stage to another, so the endpoint URL could be set during deployment.

In web.xml of my first application I describe the environmental parameter as:


<env-entry>
<description>Second WS</description>
 <env-entry-name>SECOND_SVC_URL</env-entry-name>
 <env-entry-type>java.lang.String</env-entry-type>
 <env-entry-value>http//domain/url</env-entry-value>
</env-entry> 

In the Java code I have:


Context initialContext = new InitialContext();
Context context = (Context)initialContext.lookup("java:comp/env");
Object o = context.lookup(“SECOND_SVC_URL”);
…

I have got the exception on the second line of that code:


javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
 at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:653)
 at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:257)
 at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:294)
 at javax.naming.InitialContext.lookup(InitialContext.java:361)…

Please, any help would be really appreciated.

Thank you.

Best Answer

I found the reason of my problem, just did not have time to answer my own question. That was silly of me to test my code from the main() method in one of the application classes. Definitely, you should have your web application (in my case the web service application) deployed and started first then try to get the environmental variables from the application context. Everything is working now.