Reading property files by different way and from tomcat
It's your choice. There are basically four ways:
i.e:
It's your choice. There are basically four ways:
1- To put it into "classpath", so that you can load it by ClassLoader#getResourceAsStream() with a classpath-relative path:
i.e:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));
Here filename.properties is supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g. Webapp/WEB-INF/lib, Webapp/WEB-INF/classes, Appserver/lib or JRE/lib. If the property files are webapp-specific, best is to place it in WEB-INF/classes.
If you're developing a project in an IDE, you can also drop it in src folder (the project's source folder).
2- Put it somewhere in web folder (the project's web content folder), so that you can load it by ServletContext#getResourceAsStream() with a webcontent-relative path:
i.e
Properties properties = new Properties();
properties.load(getServletContext().getResourceAsStream("/WEB-INF/filename.properties"));
Note that I have demonstrated to place the file in /WEB-INF folder, otherwise it would have been public accessible by any web browser. Also note that the ServletContext is in any HttpServlet class just accessible by the inherited GenericServlet#getServletContext().
3- Put it somewhere in local disk file system so that you can load it the usual java.io way with an absolute local disk file system path:
i.e
Properties properties = new Properties();
properties.load(new FileInputStream("/absolute/path/to/filename.properties");
4- You can alternatively also put it somewhere outside the default classpath and add it's path to the classpath of the appserver. In for example Tomcat you can configure it as shared.loader property of Tomcat/conf/catalina.properties file.
i.e
shared.loader = /home/hussain/property-source
Note: change path to your own machine.
i.e JAVA CODE.
// load properties from external resource.
Properties props = new Properties();
File configDir = new File(System.getProperty("catalina.base"));
File configFile = new File(configDir, "filename.properties"); // your property file name goes here.
InputStream stream = new FileInputStream(configFile);
props.load(stream);
String authorizerURL = props.getProperty("server.capability.authorizer.url"); // your required property goes here.
Just outweigh the advantages/disadvantages in your own opinion of maintainability.
I personally prefer putting it in the classpath outside the project - add new path to the classpath, so that I can manage it from outside and so I don't need to hard-code an absolute disk file system path in my Java code. Putting the file in the project itself would overwrite the file on every deploy and that's not useful if you intend to be able to modify the file programmatically using Properties#store() and so on. Putting the file outside the classpath in the local disk file system would require you to access it with a hard-coded path, which makes the code less portable, but it allows you to write to the file permanently using Properties#store().
i.e
shared.loader = /home/hussain/property-source
Note: change path to your own machine.
i.e JAVA CODE.
// load properties from external resource.
Properties props = new Properties();
File configDir = new File(System.getProperty("catalina.base"));
File configFile = new File(configDir, "filename.properties"); // your property file name goes here.
InputStream stream = new FileInputStream(configFile);
props.load(stream);
String authorizerURL = props.getProperty("server.capability.authorizer.url"); // your required property goes here.
Just outweigh the advantages/disadvantages in your own opinion of maintainability.
I personally prefer putting it in the classpath outside the project - add new path to the classpath, so that I can manage it from outside and so I don't need to hard-code an absolute disk file system path in my Java code. Putting the file in the project itself would overwrite the file on every deploy and that's not useful if you intend to be able to modify the file programmatically using Properties#store() and so on. Putting the file outside the classpath in the local disk file system would require you to access it with a hard-coded path, which makes the code less portable, but it allows you to write to the file permanently using Properties#store().
_______________________________________________________________________________
Syed Hussain Haider
email : ali7us(at)yahoo(dot)com
cell : +92 331 21 55 258
linkedin : http://pk.linkedin.com/in/syedhaider7
_______________________________________________________________________________