• Post Reply Bookmark Topic Watch Topic
  • New Topic

File IO using JSPs & Tomcat  RSS feed

 
C. Mitzel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm trying to write to a simple text file from a JSP using Tomcat 4. I would like the text file to reside in the same directory as the JSP. Absolute paths work fine (like "c:/file.txt"), but any attempt at an absolute path results in the file being written to C:\Documents and Settings\<username>\Start Menu\Programs\Apache Tomcat 4.0\file.txt.

Using Windows XP Home. Several questions here:
1. Why does the file end up here?
2. Tomcat appears to make all of its subdirectories read-only. Is this part of the problem, and is it possible to store *anything* in Tomcat's directory structure? (Other programs, like Word, seem to be able to save files to these directories just fine...)
3. In general, if a relative path is specified, will that be from the JSP's location, the compiled servlet's location, or some other location?
Relevant code:
<%!
public synchronized void addRecord(String filename) throws IOException{
PrintWriter out = new PrintWriter(
new FileWriter(filename, true));
out.println("abcdefg");
out.close();
}
%>
<%
addRecord("file.txt");
%>
Thanks much for your help!
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3. In general, if a relative path is specified, will that be from the JSP's location, the compiled servlet's location, or some other location?
The answer is that you don't know.
A servlet container, and the operating system it runs in, and the syatem administrator who installed it combine to set a "current directory", but it is so variable that it should never be relied on. Your Tomcat installation won't let you write into its directory from your code as that would be a major security hole.
If you just want to write something to disk, you should use the "temporary directory" provided to each web application by the container:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!