• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Writing a log file for my servlet  RSS feed

Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to write a log file for my servlet. I am using a FileWriter now and it is working fine. The problem is the file path.

now i am using absolute path , the exact hardcoded location of the file. but i donot want to do this.

I want to give a relative location to the file path, that is, inside the server/webapps folder. so that even if my server location changes i need not change the log file path.
how can i get this?

Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I use log4j which allows you to configure all of this stuff from a properties file.

As to your question:
One thing to keep in mind is that a Java webapp doesn't necessarily reside in a directory. They can also be deployed and run from a packed war archive.
The war file doesn't even need to be on the same machine.

The ServletContext.getRealPath method will find the file system path to any resource in your webapp. Using getRealPath("/") will find the root of your webapp. If, as mentioned earlier, your app hasn't been deployed as an exploded file system, get real path will return null.
For this reason, relying on getRealPath makes your webapp less portable.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!