Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a file

 
Zein Nunna
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zein Nunna
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're running from an exploded file system, ServletContext.getRealPath("/") will return the path to the root of your webapp.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic