File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
James Hodgkiss
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1609
15
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to use getRealPath() to do logging?
 
Paul Clapham
Sheriff
Pie
Posts: 20208
26
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple answer: don't do that. Write your logs somewhere else, outside the web application.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
Java Linux Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
James Hodgkiss
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1609
15
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic