This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a control servlet with the line of code String prefix = getServletContext().getRealPath("/");
that returns prefix = "C:\jakarta-tomcat-5.0.19\webapps\WebBay"
Is there anyway to interrogate the context that will return C:\jakarta-tomcat-5.0.19 only. The reason is that I can then append log to the prefix and open a logging file in C:\jakarta-tomcat-5.0.19\log.
Otherwise I guess its playing around with string functions.
Is there a reason you're not using a perfectly good, highly configurable, and very performant logging package (like log4j )
As for what you're asking, there is no built-in way to get this. You'll have to parse the String yourself. One gotcha: Tomcat can be configured to serve a web application from *anywhere*, not necessarily just from the CATALINA_HOME directories. So if you expect something of the form: c:\tomcat\webapps\foo what happens when the application is actually deployed to: c:\foo ? c:\webapps\foo?
A second gotcha is that Tomcat can be configured to run from a packed WAR file, in which case getRealPath() will return null.
One option is to specify (as part of your application's setup specs or 'administrators guide') a variable in JNDI provides an absolute path to find the log file directory. So it would be a configuration option in web.xml, and your code would then retrieve the path to use from this file.
But if we're going that far, I'd start to really look hard at log4j.
If your concern is someone being able to retrieve your logs by entering (guessing) the filename and typing it directly into a browser window, you can place your logs in WEB-INF/logs (that's what I do, with log4j). [ May 25, 2004: Message edited by: Mike Curwen ]
Joined: Jun 29, 2002
Thanks Mike, I will look at log4, I have had a brief look but all the examples I looked at set logging to standard putput not a file.
I will also look at JNDI for mapping the path name for the file.