0

I have a tomcat app that needs to write to a file in a particular directory that is different depending on where the application is deployed (windows host v linux host, etc..).

The class object that needs to know the file location is an Exception class and I don't want to have to pass the ServletContext object from the JSP all the way down to this compiled Java class object.

I'd like to be able to just have the file location specified as a parameter string in the applications context.xml file and then just comment or un-comment the correct one depending on where I'm deploying this app, but I can't seem to figure out how I can access a string parameter object from within my compiled Java class in the Tomcat webapp. I'm hoping it is something similar to how I access data source objects from the InitialContext object, but have had no luck so far getting a String object out of that...

Your help is appreciated!

H. Green
  • 735
  • 5
  • 10

1 Answers1

2

It seems odd for an exception to be concerned with writing to a file. I'd expect that to be the job of a logger which is given the exception. You can configure the logger via the servlet context, and when the exception is thrown, log the exception accordingly.

If you're convinced that your design is appropriate, perhaps you could give us more details about it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Yes its the logger component of the parent exception class for this application. It needs to log before getting to the JSP level where the Servlet Context is available to ensure it is recorded since the JSPs may not catch all exceptions. – H. Green Mar 18 '11 at 20:00
  • @Harley: What do you mean by "logger component of the parent exception class"? Typically an exception doesn't have a particular logger associated with it... – Jon Skeet Mar 18 '11 at 20:22
  • This is off topic.. Is there a way to access string parameters from the context.xml file within a compiled java class running on Tomcat without having the ServletContext object passed down from the JSP? Where and how that data is used is irrelevant. – H. Green Mar 18 '11 at 20:24
  • @Harley: It's not offtopic, because your design should probably allow the loggers to be configured appropriately. You could probably use a static variable and a context listener to get around it if you really wanted, but I'd suggest you re-examine your design instead. – Jon Skeet Mar 18 '11 at 20:25
  • I just gave this as an example use... Its design and use as a notional example is off topic from my question of: How to get a string object that is defined in the context.xml file in a compiled java class without having ServletContext passed from JSP... – H. Green Mar 18 '11 at 20:29
  • @Harley: I'd *still* try to design in a way that avoided requiring this, but you could use the suggestion from my comment: a context listener which sets a static variable, that any other code can get to. It's not nice design, but it should work... – Jon Skeet Mar 18 '11 at 20:33