Peter
Peter

Reputation: 613

How to make a directory by JSP?

I use the EclipseEE IDE to write JSP. Now there is a problem about making the directory. [problem] I write the function about making one directory in JavaBean, and I use the relative path. But the directory is always made under the directory of the EclipseEE software, rather than the position where my intent. [code]

String userDirString="./User/";
File userDir=new File(userDirString);    
if(userDir.mkdir()){
      ..........
}else{
      ..........
}

I really want program to create the directory under the WebContent directory, but it doesn't work,

I also try to the other method to implement this function-----writing it by JSP not javabean. But it does not work either.

So what should I do, now?

Upvotes: 1

Views: 1679

Answers (2)

Dave Newton
Dave Newton

Reputation: 160171

You should not use a relative filepath, relative to the app/app container/etc.

Also, the WebContent directory is a project directory, not a web app directory.

You should write to an absolute, configurable directory.

Not also that writing into a web app's directories isn't necessarily a great idea, as (1) the directory may be deleted when the app is re-deployed, and (2) isn't even possible if you deploy the app as a war file.

Upvotes: 3

Jigar Joshi
Jigar Joshi

Reputation: 240860

Following code on JSP will give you the path to webroot

String pathToWebRoot = application.getRealPath("/");

It is not preferable to write java code on jsp

Upvotes: 2

Related Questions