Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Save a file on the application root

 
Clarke Kent
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

My servlet needs to save a file in the root directory of my application.

When I opened a file using the line:



This line is inside a servlet of my application. Tt doesnt get saved on the root directory of my application. Is this supposed to work?
Or do i have to explicitly get the web application root and append the filename to have the file saved on the root directory?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"/file.txt" would mean the root of your file system to the FileWriter class.

One thing to keep in mind is that J2EE apps aren't always run from an exploded file system. Some containers will run them right from the packed war file.
In this case there is no "root" of your web application.

That being said, the way to find the root of your web app (if there is one) would be to use ServletContext.getRealPath("/").

If your app has been deployed as a war file, getRealPath will return null.
java.lang.String)" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic