• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Not able to write to propertyfile at a perticular location?

 
Ranch Hand
Posts: 256
Netbeans IDE Firefox Browser Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a application which is bundled and gets deployed as a WAR file only.
There is a property file xs.properties which I use to store my login username and password in encrypted format.

I have a passreset servlet which resets the password by encrypting & checking the old password and username and if both matches then it writes the new password and username in encrypted format to the property file xs.properties.

My trouble is how can I replace the xs.properties file with the new one. I dont know how I will be able to give a relative path which will replace the xs.properties file with the new one.
Now its creating the new xs.properties file at "webapps"(Tomcat) instead of ABC/WEB-INF/classes folder.

Can somebody let me know what shall be mine approach to make it store the new file @ ABC/WEB-INF/classes folder.

Below code creates the file @ webapps in Tomcat. In webapps folder my war file is deployed.


LAYOUT OF My WAR


Thanks
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ServletContext.getRealPath() can help you locate folder locations within the web app.

I should point out that changing files within the web app is not a great idea. It makes updating and redeployment a nightmare. I'd investigate storing the properties file outside the web app, perhaps identifying its location as a context variable.
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Netbeans IDE Firefox Browser Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

After taking your suggestion i tried to get the xs.properties file path from environment.

I made a class property and added all the custom names like below:


And I call it like below:


I start my tomcat by giving JAVA_OPTS as in VM options



But I get a null pointer exception when ever i try to access the property file.

Am I missing something or I shall do it in some other way.

Please advice.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you do any debugging? Did you try to find out at what point things were going wrong?

In any case, getBundle() loads properties from the classpath, which in this case you are not using.

You might want to check out the Properties object.

Also, why use an environment variable rather than a context variable?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should look at the documentation for the getRealPath method as using it could make your app less portable.

Depending on how your app is deployed, there may or may not be a path to the properties file in your system.

The getRealPath method takes a String argument and returns a String
representation of a file on the local file system to which a path corresponds. The
getPathTranslated method computes the real path of the pathInfo of the request.
In situations where the servlet container cannot determine a valid file path for
these methods, such as when the Web application is executed from an archive, on
a remote file system not accessible locally, or in a database, these methods must
return null
.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic