• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Writting to a local file

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use an absolute file path based off the results from System.getProperty("user.directory")
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Ranch Hand
    Posts: 5093
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 7001
    6
    Eclipse IDE Python C++ Debian Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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:
     
    author & internet detective
    Posts: 41656
    883
    Eclipse IDE VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 3178
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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...
     
    We're being followed by intergalactic spies! Quick! Take this tiny ad!
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic