Hyder Hashmi wrote:
Hi All,

When I execute the following code in my project folder, it creates the file
in my current folder(project folder).

import java.io.File;
public class CreateFile{
      File f = null;
      try{
         f = new File("test.txt");
         bool = f.createNewFile();
         System.out.println("File created: "+bool);
         }catch(Exception e){
         e.printStackTrace();
      }
}

I am now calling this class in my servlet.
The servlet is executing perfectly on the tomcat, however, the file is
getting created in the tomcat's bin folder instead of my project folder
placed in the webapps.

If I specify the path, then it creates the file on that location , however,
that will be hardcoding and will not work on the other computer.


Hyder,

apart from all the advice which you have already received, here is maybe a more general explanation, so that you would understand what you have been told so far.

When you run Tomcat, the process which is really running is a Java Virtual Machine (a JVM), which compiles and runs the code of Tomcat. Your web application runs under Tomat, which means basically that it gets compiled and run by the JVM, as a part of Tomcat. Tomcat is a Java Servlet Engine, which is an application designed to run Java servlets (like your application), according to the specifications given in the official Java Servlet Specification. Tomcat is not the only Java Servlet Engine, but they all follow these same specifications. If you want your web application to be portable to any Java servlet Engine, your application must also conform to the rules of the Java Servlet Specification.

One of these "rules" (or, in this case, the absence of one) is about the fact that you cannot rely on your web application being able to write in whatever directory (if any) is its "current" directory (even supposing that this exists at all). This "current directory" is whatever directory the JVM happens to be running in, which you have no way to know in advance. (Some day, your application may even be running inside a JVM and a Tomcat that are entirely in some non-writeable kind of memory.)

So, what everyone has been trying to tell you, is that the only way to do what you apparently want to do (have that application write something somewhere), and to do this in a portable way, is to choose a place *outside* of the Tomcat directories, some place that is guaranteed to be writeable (by the JVM which runs Tomcat and your webapp) because you created it that way (and you include this in your application's installation instructions). And to make this portable, your application has to be able to find out that location (on each system where it may be used), via some *configurable* parameter that it can read.
And there are some suggestions in previous responses, to tell you how to do 
that.

And finally, there is another reason for not writing inside your web application's directory, even if this was possible : security. If you allow Tomcat to write to your application directory, then possibly someone can take advantage of this via some misconfiguration, and overwrite your code with malicious code that would do something else which you do not want to happen at all. And you would be blamed.







---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to