Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to write to a Property file at Run time residing in the WEB-INF

 
vikram nalagampalli
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am trying to read and write to a property file residing in the WEB-INF folder. I was sucessful in reading the file. I am also partially sucessful writing to this file. The problem i am facing is, not being able to replace the same propery file in WEB-INF folder dynamically. I am wondering if this is possible to do so.
Please have a glance at my method below. The below code is generating a new file SequentialNumber.properties on the root directory of my application Server. I am wondering if there is some way to commit changes to the property file inside my WEB-INF folder.

Thanks all

public void setSequentialNum(String Num)
{
Properties p = new Properties();

p.setProperty("SequentialNum",Num);
try {
//ClassLoader cl = ClassLoader.getSystemClassLoader();

p.store(new FileOutputStream("SequentialNumber.properties"),null);
} catch (IOException ioe) {
System.out.println("error Saving properties file: " + ioe);
}

}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are looking for the ServletContext getRealPath method.
getRealPath( "/WEB-INF/SequentialNumber.properties" )
should get you a path usable for FileOutputStream
 
vikram nalagampalli
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the getServeltContext.getRealPath() but for some reason, /WEB-INF/Seq.properties cannot be found. I am wondering if there is any Permissions to write to WEB-INF.

Thank you
 
saravanan balu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont think /WEB-INF folder requires any special permission to read and write in to the files in that folder. In your previous post u r refering to Seq.properties, is the file name correct??
 
Kunaal A Trehan
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I donot think u can write the data back to any file under WEB-INF
folder

Kunal
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no restriction on writing files under WEB-INF. The only restriction observed by the servlet container is that it can't directly serve any files under WEB-INF to a request.
The whole idea of the special WEB-INF directory is that it lets the application developer have a place to read and write files that are protected from outside access AND can be addressed relatively. That lets you distribute a "web application" that will run without having any absolute file paths encoded in it.
Bill
 
Srikanth Reddy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried using Tomcat server. I am able to write to property file.
Only change I made is in the line:

p.store(new FileOutputStream(getServletContext().getRealPath("/WEB-INF/SequentialNumber.properties")),null);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic