1 Reply Latest reply on Aug 29, 2002 12:26 PM by Péter Miklós

    Using jndi.properties file instead of hard coding

    naresh maharaj Newbie

      I have a servlet in Tomcat. The servlet does a look up as follows:

      {
      Properties prop = new Properties();
      prop.setProperty"java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
      prop.setProperty"java.naming.provider.url","server:1099");
      prop.setProperty"java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");

      InitialContext initial = new InitialContext();
      Object objRef = initial.lookup("projectX/CountryRegionSession");
      }

      now if i had to do this for hundreds of servlets it would be very repetative. Can anyone explain how i use a jndi.properties file to alleaviate this problem.
      I did create a jndi.properties file and place it in the classpath on the computer where the servlet is but this didn't work.

      Thanks
      Naresh


        • 1. Re: Using jndi.properties file instead of hard coding
          Péter Miklós Newbie

          I load jndi.properties manually like this:

          ClassLoader cl = Thread.currentThread().getContextClassLoader();

          Properties props = new Properties();
          props.load(cl.getResourceAsStream(resname));

          InitialContext initial = new InitialContext(props);

          however, of course, it is also repetiative.

          You may place this code in a common class that all other classes can reach such as the ServiceLocator class in the j2ee petstore demo (java.sun.com).