Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing a log file for my servlet

 
surendar prabu
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?

regards,
Surendar
 
Ben Souther
Sheriff
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic