I have two init servlets for the following purposes:
1. One is for logging
2. The other is for loading (and parsing) XML based configuration files using Commons Digester.
When I go to %CATALINA_HOME%/conf/server.xml and inside the <HOST> tag, when I set the unpackWars attribute to false, I see a bunch of null values inside where my logging is supposed to be set!
If I set it to true, everything works well!
e.g. when I set unpackWars = false, redeploy my app, and then start Tomcat this is what I get:
My %CATALINA_HOME% is c:\DevTool\tomcat\jakarta-tomcat-5.5.9\
and when I deploy my application, its supposed to auto load these particular files inside %CATALINA_HOME%\myapp\WEB-INF, but since I didn't want to unpack my war files, it puts that weird nullWEB-INF inside the particular path!?
Does this mean that I can't have my servlets load init params unless my war files is always unpacked?
The getRealPath method only works if your application is being run from an actual file system hierarchy. If you don't unpack your war file getRealPath is supposed to return null becuase there is no real path.
public java.lang.String getRealPath(java.lang.String path)
Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
Parameters: path - a String specifying a virtual path Returns: a String specifying the real path, or null if the translation cannot be performed
[ December 21, 2006: Message edited by: Ben Souther ]
If all you need to do is read files, look at ServletContext.getResource and ServletContext.getResourceAsStream java.lang.String)" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getResource(java.lang.String)
Lastly, Log4j will look for log4j.properties in the app's classpath. I keep mine in WEB-INF/classes and it finds it.
Joined: Dec 09, 2006
Dear Mr. Souther:
Thank you very much for your helpful responses!
I tried my first two suggestions (of modifying the context.xml file and moving it under mywebapp/META-INF) and when I set the unpackWars attribute to false, again, and redeployed my app, this is what I got:
I'll try the ServletContext.getResource() and ServletContext().getResourceAsStream() methods next...
-JD [ December 21, 2006: Message edited by: James Dekker ]
Joined: Dec 09, 2006
Dear Mr. Souther & others,
I tried using the ServletContext.getResourceAsStream() and made the following changes to my Log4jInitServlet:
I decided to test by having the war file undepacked, first.
Now, when I deploy the app (with the unpackWars = true), this is what I get in my console:
The propFile is equal the following path:
Because my build script places my log4j.properties in the following place: