简体   繁体   中英

How to read properties file placed outside war?

I'm working on a web application these days. That uses only jsps and servlets. It's a small application. Right now I have placed all the DataSource details in DAO utility class. I want to place these details in a properties file that can be placed outside the war, so that depending on the environment we can change these values, without effecting the war. How can I achieve this?

Provide the file name using context param or java system parameter.

1.Context Param

<context-param>
<param-name>daofilename</param-name>
<param-value>D:\daofilename.props</param-value>
</context-param>

2.System Parameter

java -Ddao.filename=D:\daofilename.props server

This is how I do this:

Add the following to context.xml file (conf folder, in tomcat installation dir). you may change the name attribute; and in value set the path of the folder where you have your properties files.

<Environment name="config" value="C:\Program files\my app\"  
         type="java.lang.String" override="false"/>

Then in your util class, you can get the file like this: (in "java:comp/env/config" replace 'config' with the value of the "name" attribute you used in context.xml)

String folderName = null;
Properties properties = new Properties();
try {
   InitialContext context = new InitialContext();
   folderName = (String) context.lookup("java:comp/env/config");
} catch (NamingException ex) {
   System.out.println("exception in jndi lookup");
}
if(folderName != null) {
   File configFile = new File(folderName + "yourfile.properties");
   try {
       InputStream is = new FileInputStream(configFile);
       properties.load(is);
   } catch(IOException ex) {
      System.out.println("exception loading properties file");
   }
}

Hope this helps you or anyone else.

Depending on your web server, you can place the properties file in some location that is included in the classpath. For example, for some tomcat versions, that would be ${TOMCAT_BASE}/shared/classes . The webapp can then use something like the following to read the file and have it be automatically found in this location.

ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("yourfilename.cnf");

You can also name the file after your webapp's installed name and use that name in your code when loading the file from the classpath. That way, you can have the properties files for multiple webapps in the shared directory without conflicting with each other.

You've indicated that you don't have access to the ServletContext because you want the code to be in a utility class. One way you can get around this limitation is to register a ServletContextListener that creates an instance of your property file reader (since it has access to the context) and registers it so other code can use it. Something like the following:

public class MyServletContextListener extends ServletContextListener{
    public void contextInitialized(ServletContextEvent event){
        ServletContext context = event.getServletContext();
        context.setAttribute("settings", new MyPropertyReader(context));
    }

    public void contextDestroyed(ServletContextEvent event){}
    }

}

You should give property file location as java runtime property to your web container..

Such as, java -DmypropertyFile.location=c:/propertyfile.properties -jar yourContainer.jar

If you are using Tomcat you can add any properties you like to the context, look at the documentation for context.xml . All the application servers has their own way of doing this, so you will have to look around for examples of doing this in JBoss, Glassfish, etc..

Another, harder but probably better solution is to use JNDI resources .

The approach I've usually taken is for the program to assume that the configuration file is located under %user.home%/.program/config.properties .

If the file is not found (usually on first running) a copy is made from the classpath of a 'default' configuration file and placed there.

For the sake of flexibility this is usually overridable using -Dconfig=somepath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM