java - Properties setting via Servlet -
i want set data configures.properties via servlet. configures.properties locating in web-inf/classes. how i'm getting data:
public static string getdbpassword() { properties prop = new properties(); try { // load properties file inputstream in = configures.class.getresourceasstream(input_file); prop.load(in); // property value return prop.getproperty("dbpassword"); } catch (ioexception ex) { ex.printstacktrace(); } return null; } but how set? how did:
public static void setdbpassword(string str) { properties prop = new properties(); try { //load properties file inputstream in = configures.class.getresourceasstream(input_file); prop.load(in); prop.setproperty("dbpassword", str); prop.store(new fileoutputstream(input_file), null); } catch (ioexception ex) { ex.printstacktrace(); } } but i'm catching java.io.filenotfoundexception after this. think happens after prop.store(new fileoutputstream(input_file), null);. how should modify outputstream?
upd: how input_file looks:
private static final string input_file = "/config.properties";
your input_file resource path getresourceasstream resolve relative classpath, you're trying pass same string fileoutputstream constructor try , treat absolute path relative root of filesystem. these 2 different locations.
you use servletcontext.getrealpath("web-inf/classes" + input_file) path need fileoutputstream.
but higher level issue here shouldn't assume web application have write access web-inf, or directory exists on disk @ (e.g. if app running directly war rather directory unpacked on disk). if want store configuration data can change should go in file @ known location outside web app (the location of file init parameter) know have read , write permission. stops changes being overwritten when deploy new version of app.
Comments
Post a Comment