This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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,
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.
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 />email@example.com<br />----------------------------<br />There are 10 types of people<br />in the world. Those that <br />understand binary, and those<br />that don't.
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
Joined: Jan 07, 1999
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:
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.
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...