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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I am posting this in case anyone runs into similar issues that I did, this may save you a lot of grief: Condition: 1. Your NetBIOS domain name contains an ampersand " & " character.  (e.g. AT&T) 2. You've tried to run any Microsoft installation…
Often, the users face difficulty in accessing Outlook 2016 PST files on Windows 10 computer. One of the reasons behind it is the improper functioning of MS Outlook when the user tries to open it. MS Outlook suddenly stops working, or it will not op…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

569 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