Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writting to a local file

 
Nick Delauney
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I would like to write to a local file in my web application using servlets.
I'm using tomcat and my servlet is in WEB-INF/servlet. However, if I try to create/write to a file at root of the app by doing "../../logfile.html". I't does not work. Could anyone help me please.
Thanks in advance,
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an absolute file path based off the results from System.getProperty("user.directory")
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any approach you take to this is likely to be server-dependent. It is completely up to the server which directory it uses for the "current directory". Although on some installations getting the user directory might make sense, there are plenty of servlet containers runnning as users which don't have a home directory.
If you need to store files to the file system you have a few basic choices:
  • hard-code the full absolute file name in your code. This is effective only if you are completely sure that the application will only ever be run on one machine, configured in one way. You are almost always better off choosing another alternative.
  • If all you need is to write some sort of file for later use by the same application, the the servlet container is required to provide a "temporary directory" for each application to work in. This may be retrieved using File dir = (File)getServletContext().getAttribute("javax.servlet.context.tempdir");
  • If you have control over the deployment process, it can often make sense to "pass in" the full absolute path of the file as an init parameter in web.xml
  • If you can't control the deployment, you may be better off fetching the full filename from an external source once the application is running using JNDI, a database, HTTP, or whatever suits your application.


  • The bottom line is that you should never depend on the "current directory" or the "user home directory" returning anything sensible in a web application.
     
    Michael Cleary
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How does one "pass in the full absolute path of the file as an init parameter in web.xml". I need to do this for a small part of a project of mine, and I've never seen any examples of this.
    Thanks,
    Mike
     
    Jeroen Wenting
    Ranch Hand
    Posts: 5093
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    should return the real path of the WEB-INF directory for your web-app.
    Based on that you should be able to find the directory you need.
    It works on Tomcat, and will likely fail miserably on Novel Extends (SilverStream) as the latter does not use a filesystem to store files.
    Anybody's guess what it will return (and what will happen if you try to write there) if your webapp is contained in a packaged (meaning not expanded at deployment time) warfile.
    Might be interesting to try
     
    Frank Carver
    Sheriff
    Posts: 6920
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How does one "pass in the full absolute path of the file as an init parameter in web.xml". I need to do this for a small part of a project of mine, and I've never seen any examples of this.
    web.xml:

    in your servlet init method:
     
    Jeanne Boyarsky
    author & internet detective
    Marshal
    Posts: 34071
    331
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I didn't see putting the full path in a property file in Frank's list, but that's another way. Then the property file can be changed if you deploy to a different environment and the code can stay the same.
     
    Ko Ko Naing
    Ranch Hand
    Posts: 3178
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Jeanne Boyarsky:
    I didn't see putting the full path in a property file in Frank's list, but that's another way.

    What Frank have done is put the param in the web.xml as a context initialization parameters... They are available application-wide to all JSPs and servlets in that web application... We can set such params as much as you need... Hope it helps you to clear the doubt on Frank's implementation...
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic