I'm trying to get a servlet to access a value key in a .properties file. I need to create a object variable that will ask for the value of a specific key in the .properties file.
Does anyone know how this is done?
Thank you!
Accessing A Value In A .properties FileHow is this done?
Page 1 of 1
1 Replies - 4185 Views - Last Post: 10 January 2005 - 09:53 AM
Replies To: Accessing A Value In A .properties File
#2
Re: Accessing A Value In A .properties File
Posted 10 January 2005 - 09:53 AM
I'm not positive I follow you. In general to read a .properties file you will do something like:
Note that the way we've gotten the URL to the properties file requires it to be on the System classpath - i.e. the classpath that existed on startup. This may or may not be what you want in a servlet. It is possible that you'll want it on the webapp classpath.
import java.util.Properties;
import java.net.URL;
Properties properties = new Properties();
URL url = ClassLoader.getSystemResource("something.properties");
properties.load(url.openStream());
//
// the Properties class is just a HashMap of String, String
// so you can access it with
//
String value = properties.get( "key" );
Note that the way we've gotten the URL to the properties file requires it to be on the System classpath - i.e. the classpath that existed on startup. This may or may not be what you want in a servlet. It is possible that you'll want it on the webapp classpath.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|