Re: Helping setting appropriate prefix when using ServletContext.getResourceAsStream()
Well, I actually got the properties file loading properly! When I set undeployWars="false" in server.xml, I can still see my log messages inside my Tomcat console! Here's my corrected file (Log4jInitServlet): import java.io.File; import java.io.IOException; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.log4j.PropertyConfigurator; public class Log4jInitServlet extends HttpServlet { public void init() throws ServletException { Properties props = new Properties(); // String file = getServletContext().getInitParameter("log4j-init"); // File propFile = new File(file); // if (!propFile.exists()) { // System.out.println("log4j.properties file not found" + propFile.getAbsolutePath()); // } try { props.load(this.getClass().getClassLoader().getResourceAsStream("/log4j.properties")); } catch (IOException e) { e.printStackTrace(); } // PropertyConfigurator.configureAndWatch(propFile.getAbsolutePath(),1); } } Now, my problem is that I can't use the PropertyConfigurator because I placed log4j.properties in WEB-INF/classes and then just placed it into my CLASSPATH using getResourceAsStream(). If you look at my code whihc contains the invocation to PropertyConfigurator, I need a File type to place as the first parameter inside the configureAndWatch() method. Is there a way to convert this stream as File? Happy holidays! -JD On 12/22/06, Hassan Schroeder <[EMAIL PROTECTED]> wrote: On 12/22/06, James Dekker <[EMAIL PROTECTED]> wrote: > unpackWARs="true" autoDeploy="true" >xmlValidation="false" xmlNamespaceAware="false"> > > tomcat.home=C:/DevTools/tomcat/jakarta-tomcat-5.5.9 > tomcat.deployment.dir=${tomcat.home}/webapps Most of that was irrelevant :-) but the above doesn't mesh with your earlier path: C:\DevTools\tomcat\jakarta-tomcat-5.5.9\mywebapp\WEB-INF\classes\log4j.properties. :: which doesn't include a 'webapps' directory. Is that just a typo or are you setting Context atttributes (like docBase) somewhere else? -- Hassan Schroeder [EMAIL PROTECTED] - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Helping setting appropriate prefix when using ServletContext.getResourceAsStream()
On 12/22/06, James Dekker <[EMAIL PROTECTED]> wrote: tomcat.home=C:/DevTools/tomcat/jakarta-tomcat-5.5.9 tomcat.deployment.dir=${tomcat.home}/webapps Most of that was irrelevant :-) but the above doesn't mesh with your earlier path: C:\DevTools\tomcat\jakarta-tomcat-5.5.9\mywebapp\WEB-INF\classes\log4j.properties. :: which doesn't include a 'webapps' directory. Is that just a typo or are you setting Context atttributes (like docBase) somewhere else? -- Hassan Schroeder [EMAIL PROTECTED] - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Helping setting appropriate prefix when using ServletContext.getResourceAsStream()
Hassan: Thank you for your repsonse... Here's what I have in my TOMCAT_HOME/conf/server.xml file: In TOMCAT_HOME/conf/tomcat-users.xml file: http://${tomcat.server}:8080/manager tomcat.username=admin tomcat.password=admin build.properties: project.name=mywebapp build.dir=./build # Web app properties for the project webapp.name=mywebapp webapp.build.dir=${build.dir}/${webapp.name} webapp.virtual.host=localhost webapp.meta.dir=${webapp.build.dir}/META-INF # Tomcat properties tomcat.home=C:/DevTools/tomcat/jakarta-tomcat-5.5.9 tomcat.deployment.dir=${tomcat.home}/webapps tomcatTask.properties: # Tomcat Task Properties deploy=org.apache.catalina.ant.DeployTask install=org.apache.catalina.ant.InstallTask list=org.apache.catalina.ant.ListTask reload=org.apache.catalina.ant.ReloadTask remove=org.apache.catalina.ant.RemoveTask resources=org.apache.catalina.ant.ResourcesTask roles=org.apache.catalina.ant.RolesTask start=org.apache.catalina.ant.StartTask stop=org.apache.catalina.ant.StopTask undeploy=org.apache.catalina.ant.UndeployTask Finally, my build.xml file: Hope this answers your question... -JD On 12/22/06, Hassan Schroeder <[EMAIL PROTECTED]> wrote: On 12/22/06, James Dekker <[EMAIL PROTECTED]> wrote: From the three lines below: > ... %CATALINA_HOME%/mywebapp/%WEB-INF/lib > C:\DevTools\tomcat\jakarta-tomcat-5.5.9\bin\WEB-INF\classes\log4j.properties > C:\DevTools\tomcat\jakarta-tomcat-5.5.9\mywebapp\WEB-INF\classes\log4j.properties. :: I'd bet your basic configuration is wrong. What are your Host appBase and Context docBase, and where are they being set? -- Hassan Schroeder [EMAIL PROTECTED] - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Helping setting appropriate prefix when using ServletContext.getResourceAsStream()
On 12/22/06, James Dekker <[EMAIL PROTECTED]> wrote: From the three lines below: ... %CATALINA_HOME%/mywebapp/%WEB-INF/lib C:\DevTools\tomcat\jakarta-tomcat-5.5.9\bin\WEB-INF\classes\log4j.properties C:\DevTools\tomcat\jakarta-tomcat-5.5.9\mywebapp\WEB-INF\classes\log4j.properties. :: I'd bet your basic configuration is wrong. What are your Host appBase and Context docBase, and where are they being set? -- Hassan Schroeder [EMAIL PROTECTED] - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Helping setting appropriate prefix when using ServletContext.getResourceAsStream()
Dear Chuck Caldarale & Others, Thank you for your response! Chuck - Your e-mail was very informative... I did remove the servlet-apis from my %CATALINA_HOME%/mywebapp/%WEB-INF/lib directory. The only reason I had it there was because I use Eclipse for development and its easier to configure the build path by clicking on "Add Jar" rather than clicking on "Add External Jar". But I took your advice and removed it and now use "Add External Jar". Here's my situation... I changed my Ant build script to move the log4j.properties file into my WEB-INF/classes dir. Also, my log4j-1.2.9.jar gets moved inside my WEB-INF/lib dir. But for some odd reason, when I run Tomcat, it doesn't seem to know where my log4j.properties file is! My Log4jInitServlet seems to have the path wrong! It looks inside: C:\DevTools\tomcat\jakarta-tomcat-5.5.9\bin\WEB-INF\classes\log4j.properties instead of: C:\DevTools\tomcat\jakarta-tomcat-5.5.9\mywebapp\WEB-INF\classes\log4j.properties. Furthermore, it throws a NullPointerException but at the same time, right afterwards, it logs my XmlConfigInitServlet (I have it set there!). Here's what my console says: Dec 22, 2006 9:45:05 AM org.apache.catalina.startup.HostConfig deployWAR INFO: Deploying web application archive mywebapp.war propFile path is: WEB-INF\classes\log4j.properties log4j.properties not found, C:\DevTools\tomcat\jakarta-tomcat-5.5.9\bin\WEB-INF\classes\log4j.properties 2006-12-22 09:45:07,653 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/mywebapp]] - StandardWrapper.Throwable java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:365) at java.util.Properties.load(Properties.java:293) at com.acme.mywebapp.logging.Log4jInitServlet.init(Log4jInitServlet.java:27) at javax.servlet.GenericServlet.init(GenericServlet.java:211) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:925) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3857) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4118) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:788) at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:677) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:473) at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1102) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1020) at org.apache.catalina.core.StandardHost.start(StandardHost.java:718) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442) at org.apache.catalina.core.StandardService.start(StandardService.java:450) at org.apache.catalina.core.StandardServer.start(StandardServer.java:683) at org.apache.catalina.startup.Catalina.start(Catalina.java:537) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:271) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:409) 2006-12-22 09:45:07,669 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/mywebapp]] - Servlet /mywebapp threw load() exception java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:365) at java.util.Properties.load(Properties.java:293) at com.acme.mywebapp.logging.Log4jInitServlet.init(Log4jInitServlet.java:27) at javax.servlet.GenericServlet.init(GenericServlet.java:211) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:925) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3857) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4118) at org.apache.catal
RE: Helping setting appropriate prefix when using ServletContext.getResourceAsStream()
> From: James Dekker [mailto:[EMAIL PROTECTED] > Subject: Helping setting appropriate prefix when using > ServletContext.getResourceAsStream() Where to begin... > // String prefix = getServletContext().getRealPath("/"); You apparently already figured out the above was a bad idea, as is any direct I/O request from inside a webapp. When your webapp is deployed as a .war, it has no access to the structure inside the .war file other than via the classloader (which includes the getResourceAsStream() API). Consequently, much of the rest of your code in the servlet is inappropriate. As far as where to place log4j.properties, the Tomcat doc states: "You would place a similar log4j.properties file in your web application's WEB-INF/classes folder, and log4j1.2.8.jar into WEB-INF/lib. Then specify your package level logging." See: http://tomcat.apache.org/tomcat-5.5-doc/logging.html for more info. > > MySampleAppServlet > /app > Note that the above will allow only the URI http[s]://host[:port]/myapp/app to reach your servlet. If you want all requests for the myapp context to be processed by your servlet, change the url-pattern to just "/" (without the quotes). >WEB-INF\log4j.properties >WEB-INF\attributes-config.xml Use forward slashes in the config files, even on Windows. > Dec 21, 2006 4:53:16 PM > org.apache.catalina.loader.WebappClassLoader.validateJarFile > INFO: > validateJarFile(C:\DevTools\tomcat\jakarta-tomcat-5.5.9\webapps\ > affiliates\WEB-INF\lib\servlet-api.jar) > - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending > class: javax/servlet/Servlet.class Don't put servlet-api.jar into any of your webapps, since Tomcat already supplies it in common/lib. No class should ever appear in more than one place in any given branch of the classloader tree: Bootstrap | System | Common / \ Catalina Shared / \ Webapp1 Webapp2 ... - Chuck THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers. - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Helping setting appropriate prefix when using ServletContext.getResourceAsStream()
Hello there, I am using JDK 1.5 & Tomcat 5.5.9 on WinXP... Set up my init servlet which is supposed to load my log4j.properties file from myapp/WEB-INF/log4j.properties. Here's my init servlet's source listing: -- package com.acme.logging; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.log4j.PropertyConfigurator; public class Log4jInitServlet extends HttpServlet { public void init() throws ServletException { // String prefix = getServletContext().getRealPath("/"); Properties p = new Properties(); String prefix = "./"; String file = getInitParameter("log4j-init-file"); File propFile = new File(prefix+file); System.out.println("propFile path is: " + propFile); try { InputStream is = getServletContext().getResourceAsStream("propFile"); if(!propFile.exists()){ System.out.println("log4j.properties not found, " + propFile.getAbsolutePath()); } p.load(is); is.close(); } catch(IOException e) { e.printStackTrace(); } PropertyConfigurator.configureAndWatch(propFile.getAbsolutePath(),1); } } Here's my web.xml file: -- http://java.sun.com/dtd/web-app_2_3.dtd";> MySampleApp MySampleApp MySampleAppServlet com.acme.MySampleAppServlet MySampleAppServlet /app log4j-init com.acme.logging.Log4jInitServlet log4j-init-file WEB-INF\log4j.properties 1 xml-config-init com.acme.config.XmlConfigInitServlet xml-config-file WEB-INF\attributes-config.xml 2 -- Now, when I deploy my app and run Tomcat this is what it says in the Console: -- Dec 21, 2006 4:53:14 PM org.apache.coyote.http11.Http11Protocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 Dec 21, 2006 4:53:14 PM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 847 ms Dec 21, 2006 4:53:14 PM org.apache.catalina.core.StandardService start INFO: Starting service Catalina Dec 21, 2006 4:53:14 PM org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine: Apache Tomcat/5.5.9 Dec 21, 2006 4:53:14 PM org.apache.catalina.core.StandardHost start INFO: XML validation disabled Dec 21, 2006 4:53:15 PM org.apache.catalina.startup.HostConfig deployWAR INFO: Deploying web application archive affiliates.war Dec 21, 2006 4:53:16 PM org.apache.catalina.loader.WebappClassLoader validateJarFile INFO: validateJarFile(C:\DevTools\tomcat\jakarta-tomcat-5.5.9\webapps\affiliates\WEB-INF\lib\servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class log4j:WARN No appenders could be found for logger (org.apache.catalina.session.ManagerBase). log4j:WARN Please initialize the log4j system properly. propFile path is: .\WEB-INF\log4j.properties log4j.properties not found, C:\DevTools\tomcat\jakarta-tomcat-5.5.9\bin\.\WEB-INF\log4j.properties Dec 21, 2006 4:53:18 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 3778 ms -- How should I set the prefix in my Log4jInitServlet to point to: %CATALINA_HOME%/webapps/mywebapp/ Thank you so much! -JD - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]