Re: File getting created in bin folder instead of project folder

2015-02-09 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Hyder,

On 2/8/15 11:28 PM, Hyder Hashmi wrote:
 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.

1. Use ServletContet.getRealPath(/path/inside/webapp).

2. Don't ever use ServletContext.getRealPath

The reason you shouldn't use getRealPath is because your web
application might not be deployed in an unexploded WAR file, and
therefore getRealPath will return null in that case, so you won't be
able to do anything with that path.

Instead, you need to use some other mechanism for writing files. What
do you plan to do with these files once they have been written? If you
intend to serve them back to clients using Tomcat's DefaultServlet,
that usually ends badly due to resource-caching, etc.

We have some servlets that write files to the filesystem for auditing
purposes. We have a configuration file that specifies where those
files should be written, and we load that file on startup. Perhaps
reading from a configuration file, or even from the servlet's
init-params, you can build a correct filesystem path and write your
files there.

- -chris
-BEGIN PGP SIGNATURE-
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJU2MzTAAoJEBzwKT+lPKRYV08QAIam6R7sR9c1YmPgkkuqEYM2
xoGUzcfnR6AFbRhTwu95W1k+yxHUqV+zaq6+z9ULBjXKVEO+0VS55EL9CTwDW1Xb
p64zP1zOdGq2pGJswIwj4voq94ZKQPKp6HK3hnuaA1RsskRY2Wvi7eiwskbw5KNi
AWI4iYHkJGQySUc5HnwwNLoZYqcpO7q5p91b/2Pk3r2g1hGBFZFM4KOHGfEjRgOH
VOUZnXmUEeJ7vIiOfbmJmvIZL/mBRdlzulVA7ID9JbiJs3id/WdzLun0Eh/DNnaX
ur1mhL+tZpuE+xy6WgkPAfMqkkzJ8R4jkn7c0YJZX+rmdHDJYj0WgjVYq53nBrMG
ogd3QHE32SesB6JCabveas57dQTLO6cykBMKZ7kWOkS1t+GNjemPWUKUIdpFb+pM
jqBOuXOOgRUPtqGO7b83B7QJILz1JvyCWHsfNzbnqi1WB/98AT2F0KrquNWOpJLF
IIGNBrpdeIh0Wlr8t5ZmOr806hnItWEHB1pqwCvBXok2jbllZrBaC+OqwloWah09
V3IfyHJKvux7BNA83SSK+wpHAYR2W7Esn1lYdxqmxYv4q4FqPbvATgnYVfR6wwVu
WD4nQVgNQl2CReDiT53SnQK6xKPMtCYYIa0jYybtB+0xNjKz3aemSW82JYvs+oj+
5qye5crH7bGxRs4cI09g
=LeVi
-END PGP SIGNATURE-

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



Re: File getting created in bin folder instead of project folder

2015-02-09 Thread André Warnier

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



Re: File getting created in bin folder instead of project folder

2015-02-08 Thread Tim Watts
On Mon, 2015-02-09 at 09:58 +0530, 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();
   }
 }
 
First, there's no way that compiles.  Perhaps that block is supposed to
be wrapped in a method?

 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.
 

Second, you asked this question last week and you got several replies
offering guidance.  What advice, if any, did you follow and how did it
turn out?  (Hint: it's annoying when people take time to help and you
don't listen to them but want more help.)

As one of the replies suggested, probably the best approach is to set an
absolute path in either a system or context property.  Then obtain that
value during servlet init and pass it to the CreateFile class somehow
(via a constructor or a set method, or as a param to the create method
etc.).

--- Tim

 Please help.
 Thanks in advance
 Hyder!



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



File getting created in bin folder instead of project folder

2015-02-08 Thread Hyder Hashmi
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.

Please help.
Thanks in advance
Hyder!