Logging Problem with unpackWARs="false"
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Am using JDK 1.5 and Tomcat 5.5.9...
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?
Here's some code for your review:
Has anyone experienced the same kind of thing?
Thanks again, JavaRanch rocks!
Happy holidays,
JD
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
From: http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)
getRealPath
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
That was such a good find (on your part)!
Thank you so much!
After looking at this article
It seems like the only three ways to do is are:
1. In %CATALINA_HOME%/conf/context.xml
set the following attribute to true by doing this:
antiJARLocking="true" inside the <Context> tag
2. Do step1 and then move a copy of your context.xml file to %CATALINA_HOME%/yourapp/META-INF/
3. Use the ClassLoader to load the XML config file and log4j.properties file instead of using getServletContext().getRealPath(). Can anyone explain how to use the ClassLoader to do this?
Would it be something like this:
Can anyone suggest an alternative to my getServletContext().getRealPath()?
Does anyone know of a better way than the ones listed above?
Sincerely,
JD
[ December 21, 2006: Message edited by: James Dekker ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
If all you need to do is read files, look at
ServletContext.getResource
and ServletContext.getResourceAsStream
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getResource(java.lang.String)
Also look at:
http://wiki.apache.org/tomcat/HowTo#head-45c3314139cb900ddd43dde2ff671532e6e844bc
Lastly, Log4j will look for log4j.properties in the app's classpath.
I keep mine in WEB-INF/classes and it finds it.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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...
Happy holidays!
-JD
[ December 21, 2006: Message edited by: James Dekker ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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:
%CATALINA_HOME%/webapps/mywebapp/WEB-INF/
Because my build script places my log4j.properties in the following place:
%CATALINA_HOME%/webapps/mywebapp/WEB-INF/log4j.properties
What could I be possibly be doing wrong?
Sincerely yours,
JD
| The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |








