• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing to a file through Servlet

 
Rohit Malhotra
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to log all the events in the application into a file log.txt
How can a write to a file through a servlet. I do need the absolute path of the file. like for example the file is in docs folder of the base directory of my application project.

BASE_DIR
- WEB-INF/CLASSES
- WEB-INF/Web.xml
- SRC
- DOCS/Log.txt

How can i write to this file log.txt through my Servlet?
Thanking in advance,
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServletContext has a log methods with which you can log events to files.

You also get log methods in your servlet through GenericServlet-->HttpServlet.

For absolute path you can use getRealPath() from ServletContext. But this method will return null if you pack your application as a war file.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition, Filters meant for these kinda stuff. You can also go for some libraries like Log4J or jakarta commons logging. And you can always search the web for some working examples.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!