I. Joseph I. Joseph - 1 year ago 65
Java Question

Creating A File Resource Using Tomcat JNDI

I need to read an

file from a servlet application using
container. The file can not be included in my
so it can't be under webapps or
root folder in any ways. The file has to be somewhere in the folder. I also can not use
to read the properties file. Only option I have is to define a
name for a folder / directory and look that
properties during runtime to find the folder location to read the file. Is thee any working example out there?

Answer Source

I have chalked out a solution for myself reading the following similar posts and articles.

The Context Container

Reading a global variable from tomcat with JNDI. Example not working

java:comp/env is not bound

I have defined a Environment inside my Context for my web application under \conf\Catalina\localhost\mywebapp.xml as follows....

    <Environment name="propertiesfilelocation" value="E:\\tmp\\application.properties"
     type="java.lang.String" override="false"/>

Then accessed my properties file using a JNDI lookup to get the file name.

   Context ctx  = new InitialContext();
   Context envCtx = (Context)ctx.lookup("java:comp/env");
   String propertiesFileLocation = (String) envCtx.lookup("propertiesfilelocation");
   LOGGER.info("String property === " + propertiesFileLocation);

   properties.load(new FileInputStream(propertiesFileLocation));

@home : Yes you are right it involved FileInputStream. However, I am happy with the solution because I am no longer hard coding my folder location inside my Java code which makes my app more portable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download