Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

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

 
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);
}

}
 
Author and all-around good cowpoke
Posts: 13078
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
 
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??
 
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
Posts: 13078
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
 
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);
 
Not so fast naughty spawn! I want you to know about
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic