wood burning stoves 2.0*
The moose likes Servlets and the fly likes Not able to write to propertyfile at a perticular location? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Not able to write to propertyfile at a perticular location?" Watch "Not able to write to propertyfile at a perticular location?" New topic
Author

Not able to write to propertyfile at a perticular location?

Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 247

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


Regards, Ricky
Oracle Weblogic 10g Certified Expert
TechPaste.Com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60074
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 247

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60074
    
  65

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?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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
.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to write to propertyfile at a perticular location?
 
Similar Threads
HTTP Status 404 problem
ant not deploying on Tomcat 7
Referencing an image in Jasper Report
Deploying a servlet to a Tomcat server I don't own
Problem in Struts : Error 404 : servlet action as unavailable