File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Writing files into WEB-INF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Writing files into WEB-INF" Watch "Writing files into WEB-INF" New topic
Author

Writing files into WEB-INF

Brooke Maddox
Ranch Hand

Joined: Nov 23, 2012
Posts: 42
I have a method in a Java class that I would like to use to write to a text file which is located in my WEB-INF dir. I think I am having trouble getting the correct path for this file. I am calling the method in my Servlet and using Servlet Context in order to get the path but I have yet to write anything to the text file. Here is my code
Servlet

Class
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

It is a phenomenally bad idea to write anything into the web app folders.

What happens when you need to redeploy the app and all your changed/written files are wiped out?

Why are you doing this?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Brooke Maddox
Ranch Hand

Joined: Nov 23, 2012
Posts: 42
For my assignment I have to use a text file that I can read from and write to. I thought that putting it in the WEB-INF would keep it hidden from the user.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

It will, but is unmaintainable. Writing it somewhere outside the web app ail also "hide it" from the user.
Brooke Maddox
Ranch Hand

Joined: Nov 23, 2012
Posts: 42
So do you suggest that I have a folder outside the WEB-INF that is dedicated to my .txt file? Can I put the .txt file in the same folder as my Bean or Class? Do I still need to use the getServletContext() in my Servlet?
Brooke Maddox
Ranch Hand

Joined: Nov 23, 2012
Posts: 42
I got it! Thanks again Bear
 
 
subject: Writing files into WEB-INF
 
Similar Threads
How do I copy and paste to a text file?
Split a File According to a String in the File
Servlet Parameters null
Printing Text file in Java
Writing to a text file