jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes writing to a file in /WEB-INF/ from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "writing to a file in /WEB-INF/ from a servlet" Watch "writing to a file in /WEB-INF/ from a servlet" New topic
Author

writing to a file in /WEB-INF/ from a servlet

Tom Kowalski
Ranch Hand

Joined: Feb 17, 2009
Posts: 72
Hi guys, I have got short and simple question. Can I write to a file which is in /WEB-INF/ from a servlet? I have got an application, in which in one part I'm reading from a file, using context.getResourceAsStream(fileName), and in another part I wanted to write TO a file, but I can't find a method thanks to which I could to this. Is there some kind of method which could help me?


SCJP, SCWCD, OCUP
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Yes. You can write to a file there.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Tom Kowalski
Ranch Hand

Joined: Feb 17, 2009
Posts: 72
And could somebody write how? ^^ Please...

I have been also trying something like this:


but GlassFish is throwing an error: java.net.UnknownServiceException: protocol doesn't support output
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Just open a file and write to it.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I'd suggest you take a look at the File I-O package.
Tom Kowalski
Ranch Hand

Joined: Feb 17, 2009
Posts: 72
I just needed a portable way to get address of this file, It seems to me that this should work context.getRealPath(fileName).

Anyway, now I'm wondering, if context.getResourceAsStream(fileName) has got some advantages over normal IO functions.

Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It works for resources--anything on the classpath, including things packaged inside JAR files. File IO doesn't.
Tom Kowalski
Ranch Hand

Joined: Feb 17, 2009
Posts: 72
Ok, sorry, last thing (I know David that I'm making you angry ^^)

My idea with


is not working good, because it`s returning something like this:
F:\WI\build\web\WEB-INF\oceny\plik.txt

and it should return this:
F:\WI\web\WEB-INF\oceny\plik.txt (without 'build' inside)

(I'm using NetBeans).
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If that's the path it's returning then it's probable that it's correct, and NetBeans is deploying from its build directory.
Tom Kowalski
Ranch Hand

Joined: Feb 17, 2009
Posts: 72
Thanks, sorry for problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing to a file in /WEB-INF/ from a servlet
 
Similar Threads
path to applicationContext.xml
Servlet config in servlets
HFSJ page89
creating a file from WEB-INF
Internal Servlet Error ?