Click to See Complete Forum and Search --> : Accessing properties files


vinays84
04-12-2007, 02:53 PM
How would one access a properties file from java code in a webapp?

Khalid Ali
04-13-2007, 11:04 AM
put the properties file in the classes folder and access it from ur web aplications context.

vinays84
04-13-2007, 06:33 PM
could you provide a short example with the code that would appear in context xml and java code that accesses it?

Khalid Ali
04-14-2007, 06:01 AM
There are probably multiple ways to access a file.
The 2 that come to mind right now are
ClassLoader loader= this.getClass().getClassLoader();
Properties props = new Properties();
props.load(loader.getResourceAsStream("myfile.properties");

The above will attempt to read from classes folder.

Second approach may be
getRealPathgetServletContext().getRealPath("/WEB-INF/myfile.properties");

All of this is right of my memory..:-) so you will have to read API to see the correct syntax.

vinays84
04-16-2007, 12:37 PM
Thank you, you're first approach works well.