| Author |
Writting to a local file
|
Nick Delauney
Ranch Hand
Joined: Sep 28, 2002
Posts: 43
|
|
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,
|
N.D:"Anything worth having, takes time to get"
|
 |
Ray Stojonic
Ranch Hand
Joined: Aug 08, 2003
Posts: 326
|
|
|
Use an absolute file path based off the results from System.getProperty("user.directory")
|
 |
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6919
|
|
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.
|
Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
|
 |
Michael Cleary
Ranch Hand
Joined: Jul 29, 2003
Posts: 93
|
|
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
|
Mike<br />SCJP 1.4<br />----------------------------<br />mdcleary@earthlink.net<br />----------------------------<br />There are 10 types of people<br />in the world. Those that <br />understand binary, and those<br />that don't.
|
 |
Jeroen Wenting
Ranch Hand
Joined: Oct 12, 2000
Posts: 5093
|
|
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
|
42
|
 |
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6919
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26496
|
|
|
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.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Ko Ko Naing
Ranch Hand
Joined: Jun 08, 2002
Posts: 3178
|
|
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...
|
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
|
 |
 |
|
|
subject: Writting to a local file
|
|
|