Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Tomcat - JSP/Servlet container
Struts - A MVC web framework
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
JSP - A mailing list about Java Server Pages specification and reference
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Oracle Connection Pooling in 3 2 2
Servlet : Session invalidate
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Tomcat and webapplication specific java library path
Running a Simple JMS Example
Mapping in workers2 properties
org apache jasper JasperException
Cannot find message resources under key org apache struts action
   MESSAGE
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
url string for connecting jboss to oracle
Value attribute of <html:checkbox
javax servlet ServletException: BeanUtils populate
HTTP Status 404 The requested resource is not available
5 0 18: Windows XP Pro vs Windows 2000
 
Helping setting appropriate prefix when using ServletContext.getResourceAsStream

Helping setting appropriate prefix when using ServletContext.getResourceAsStream

2006-12-21       - By James Dekker

 Back
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 Source code of java.io.File;
import java.io.IOException Source code of java.io.IOException;
import java.io.InputStream Source code of java.io.InputStream;
import java.util.Properties Source code of java.util.Properties;

import javax.servlet.ServletException Source code of javax.servlet.ServletException;
import javax.servlet.http.HttpServlet Source code of javax.servlet.http.HttpServlet;

import org.apache.log4j.PropertyConfigurator Source code of 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(),10000
);
   }
}

Here's my web.xml file:

-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ------
-- ---- ---- ---- -----
<!DOCTYPE web-app
   PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

   <display-name>MySampleApp</display-name>
   <description>
       MySampleApp
   </description>

   <servlet>
     <servlet-name>MySampleAppServlet</servlet-name>
     <servlet-class>com.acme.MySampleAppServlet</servlet-class>
   </servlet>

   <servlet-mapping>
     <servlet-name>MySampleAppServlet</servlet-name>
     <url-pattern>/app</url-pattern>
   </servlet-mapping>

   <servlet>
      <servlet-name>log4j-init</servlet-name>
      <servlet-class>com.acme.logging.Log4jInitServlet</servlet-class>
      <init-param>
          <param-name>log4j-init-file</param-name>
          <param-value>WEB-INF\log4j.properties</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>  

  <servlet>
      <servlet-name>xml-config-init</servlet-name>
      <servlet-class>com.acme.config.XmlConfigInitServlet</servlet-class>
      <init-param>
          <param-name>xml-config-file</param-name>
          <param-value>WEB-INF\attributes-config.xml</param-value>
      </init-param>
      <load-on-startup>2</load-on-startup>
  </servlet>
</web-app>

-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ------
-- ---- ---- ---- -----

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 Source code of org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080 (See http://ttp-8080.ora-code.com)
Dec 21, 2006 4:53:14 PM org.apache.catalina.startup.Catalina Source code of org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 847 ms
Dec 21, 2006 4:53:14 PM org.apache.catalina.core.StandardService Source code of org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Dec 21, 2006 4:53:14 PM org.apache.catalina.core.StandardEngine Source code of 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 Source code of org.apache.catalina.core.StandardHost start
INFO: XML validation disabled
Dec 21, 2006 4:53:15 PM org.apache.catalina.startup.HostConfig Source code of 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 Source code of org.apache.catalina.loader.WebappClassLoader
validateJarFile
INFO: validateJarFile(C:\DevTools\tomcat\jakarta-tomcat-5 (See http://cat-5.ora-code.com).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 Source code of javax/servlet/Servlet
log4j:WARN No appenders could be found for logger
(org.apache.catalina.session.ManagerBase Source code of 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 (See http://cat-5.ora-code.com).5.9\bin\.\WEB-INF\log4j.properties
Dec 21, 2006 4:53:18 PM org.apache.catalina.startup.Catalina Source code of 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@(protected)
To unsubscribe, e-mail: users-unsubscribe@(protected)
For additional commands, e-mail: users-help@(protected)


©2008 junlu.com - Jax Systems, LLC, U.S.A.