File I/O in a JSP in Tomcat

Posted on 2005-03-22
Medium Priority
Last Modified: 2008-03-03
I have a JSP page that was initially developed in JRun and I was able to access the file:


by using the following relative reference:

BufferedWriter out1 = new BufferedWriter(new FileWriter(".\SJRF\logs\JRC_4B0DBFAA59087A64ED279B60FEBBE837.log",true));

When I try the same thing in Tomcat 5.5.4, it is unable to find the path. I know ideally I should be using the servlet engine logging mechanism, but for now I'd like to know if there's some way in tomcat to reference a file from a location relative to where the webapp is deployed (or even relative to where tomcat is installed).
Question by:opike
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 92

Accepted Solution

objects earned 700 total points
ID: 13608529
LVL 29

Assisted Solution

bloodredsun earned 300 total points
ID: 13610203
To expand on objects comment:

ServletContext app = getServletContext();
String path = app.getRealPath("/WEB-INF/logs/mylog.txt");
//returns <tomcat_home>/webapps/<your_webspp>/WEB-INF/logs/mylog.txt
//e.g. C:\programs\tomcat4_1\webapps\myapp\WEB-INF\logs\mylog.txt

//do your outputting to file here

To get the root of your webapp just call
String path = app.getRealPath("/");

Author Comment

ID: 13658324
I split the points. Gave objects a few more since he provided the initial answer.
LVL 92

Expert Comment

ID: 13658343
thanks mate :)

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question