• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Writing to a file through Servlet

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic