On Mon, 23 Jul 2001, Monte Gardner wrote:

| There were some questions in the archives about how to write
| data from HTML forms to a file on the server.  Specifically,
| some people wanted to know how to get the path to a file in
| the directory in which the Servlet was located.  I had
| the same problem, and I found that the servlet was throwing
| a fileNotFoundException when it looked for the file "names.txt",
| even though names.txt was in the same directory.  I had a
| suspicion that the server was actually running the servlet
| in another directory. so I inserted the following code into
| doGet();
|
|                  String defaultPath = System.getProperty("user.dir");
|                 File here = new File(defaultPath);
|                 String[] contents = here.list();
|                 for (int index = 0; index < contents.length; index++)
|                 {
|                         out.println(contents[index] + "<br>");
|                 }
|                 out.println("Directory: " + defaultPath + "<br>");
|
| this allowed me to figure out where the servlet was beeing run
| (about 3 directories above my servlet directory), wich allowed me
| to create a fileWriter to the appropriate path.

This is not a very good approach, since it's extremely unportable! ;)

You should basically _not_ write to files! And File() is not the best
option anyway, since this is even more unportable. (Use URLs, I believe,
as your File(), and open streams on them. This because e.g. TC4 uses some
JNDI to handle all file requests, and they truly mess with your
file-"upload" possibilities..)


--
Mvh,
Endre

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to