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 How to create an output/log file from a servlet contained in a WAR? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How to create an output/log file from a servlet contained in a WAR?" Watch "How to create an output/log file from a servlet contained in a WAR?" New topic
Author

How to create an output/log file from a servlet contained in a WAR?

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Hiya,

The logging in my webapp works fine... until it is packaged in a WAR. When used from a WAR, this code:



...gives null. Is there any other way I can access it without having to specify and absolute locations anywhere (as my webapp needs to work from the WAR on any machine without pre-configuration).

Many thanks,
James
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 983
    
  13

That only works if the war file is unpacked. If the app runs from a packed war, there is no "real" file system for it evaluate.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Why do you need to use getRealPath() to do logging?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Simple answer: don't do that. Write your logs somewhere else, outside the web application.
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Thanks for the replies. No, I don't have to use getRealPath() and I'm happy to write the files outside the web application. How then, could my servlet create a subdirectory in the root directory then output the files there (without hardcoding in any absolute paths)?
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

Please aware that this method will only work when your web application is deployed in an exploded way, if it was deployed in a war format the getRealPath() method just return null.

So I hope it may work for you, String path = new File(".").getCanonicalPath();

No pain, No gain.
OCJP 1.6
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Thanks for that, but my web app is deployed in an unexploded way.

Bearing that in mind, is there any way my servlet can create a subdirectory in the root directory then output the files there (without hardcoding any absolute paths into my web app)?
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 983
    
  13

James Hodgkiss wrote:Thanks for that, but my web app is deployed in an unexploded way.

Bearing that in mind, is there any way my servlet can create a subdirectory in the root directory then output the files there (without hardcoding any absolute paths into my web app)?


It's unlikely. The user id that is running the web container is not likely a root user (it shouldn't be) so it won't be able to create directories off the root. Consider using an existing directory such as /var/log/.

I'm still not sure why you need to create a directory for logging. Have you created your own logging framework? I've used log4j and java.util.Logging and both put the log files in /var/log/tomcat5.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

You cannot create files or folders inside the war. And generally, even oif you could, you shouldn't create files or folders inside the web app. What happens to them when it comes time to redeploy or update the app?

For logging I recommend the use of log4j.
 
GeeCON Prague 2014
 
subject: How to create an output/log file from a servlet contained in a WAR?