File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to write to a Property file at Run time residing in the WEB-INF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to write to a Property file at Run time residing in the WEB-INF" Watch "How to write to a Property file at Run time residing in the WEB-INF" New topic
Author

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

vikram nalagampalli
Ranch Hand

Joined: Oct 08, 2001
Posts: 91
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

Joined: Mar 22, 2000
Posts: 12778
    
    5
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

Joined: Oct 08, 2001
Posts: 91
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

Joined: May 19, 2000
Posts: 49
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

Joined: Feb 04, 2004
Posts: 63
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

Joined: Mar 22, 2000
Posts: 12778
    
    5
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

Joined: Jun 05, 2004
Posts: 10
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);


Srikanth<br />SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write to a Property file at Run time residing in the WEB-INF