aspose file tools*
The moose likes Servlets and the fly likes Creating a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Creating a file" Watch "Creating a file" New topic
Author

Creating a file

Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Guys,

I am wanting to create a file on the webserver.

I have some text from a text area from the html, this needs to be stored in a file on the webserver, any tips on how to go about it. I have no problems doing this locally, and i know how to use the class File etc..

Any tutorials, links, code snipets etc. welcome

Thanks in advance
Zein
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It's no different on a webserver than on a local machine.
The trick is knowing where to write the file.

I recommend using a context-init-param or a servlet-init-param to configure the path. Trying to use the current working dir or trying to dynamically determine the location of the webapp itself are recipies for headaches.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
What do i need to do with the context-init-param or a servlet-init-param?

What i am trying to do is have a directory structure something like

/uni/var1/var2/file.txt

Where uni is an existing directory an var1 and var2 are varibles that I will retrieve via request.getParameter(....);
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
why dont you do a File.mkdirs() to make the required directories and then use the File related streams to do it in your servlet?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Zein Nunna:
What do i need to do with the context-init-param or a servlet-init-param?
What i am trying to do is have a directory structure something like
/uni/var1/var2/file.txt
Where uni is an existing directory an var1 and var2 are varibles that I will retrieve via request.getParameter(....);


I mentioned configuring the file's path on the server as an init-param because a j2ee app will not exist on the file system if it is run from a packed war file. ServletContext.getRealPath("/") will return null. If you plan on deploying to a container that allows you to run from an exploded file system and you don't care about portability then there is no need to concern yourself with this issue.
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Guys,

Thanks for the advice above.

I dont think i'll be needing a .war file, the app will run from just my computer, and i just need to demonstarte once - and after that it will probably not be used.

RE: the issue above, well it kinda works, but doesnt work at the same time.

I have a servelet that creates a directory structure, using method as follows:

new File(/test/one).mkdirs();

Locally this works fine.

In the servlet I have an if statement (as shown below) which directs me to different sites depending on if the directory structure exists or not.

if (new File(/test/one).mkdirs())
Site A
else
Site B

So the 1st time round, it should be site A and then subsequent times site B, this works as it should, however the directory structre isnt visible in my root directory, sooo where is it then? I know its being created (due to the if statements)?

Do i need to configure the path or something?

Thnaks in advance
Zein
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you're running from an exploded file system, ServletContext.getRealPath("/") will return the path to the root of your webapp.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Creating a file