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
 
How to load XML config file properly from WEB-INF?

How to load XML config file properly from WEB-INF?

2006-12-22       - By James Dekker

 Back
Reply:     1     2  

Hi,

If you've been following my previous posts, you'll notice that I had a
problem of trying to load a properties file (stored under WEB-INF),
until I edited my build script and made it place the log4j.properties
file under WEB-INF/classes, and then used the following code to load
in the properties file:

public class Log4jInitServlet extends HttpServlet {
      public void init() throws ServletException  {
               Properties  props  = new Properties();
               try {
     props.load(this.getClass().getClassLoader().getResourceAsStream("/log4j
.properties"));
        } catch (IOException e) {
          e.printStackTrace();
        }
   }

Although, this works for loading property files into my webapp's
CLASSPATH, my new question (just for those who think that I am trying
to post the same question twice, which I am not), is this:

I also have a servlet which uses init params to load & parse XML
config files (which are located under: %TOMCAT_HOME%/mywebapp/WEB-INF/
)

My new question is:

How do I use the getResourceAsStream() method to load this particular
XML file which is not located under WEB-INF/classes? Its located under
just WEB-INF.

Also is there a way to convert this InputStream into a File?

Here's my original piece of code (which works when
undeployWars="true", I am trying to get it to work when
undeployWars="false"):

   public class XmlConfigInitServlet extends HttpServlet {
     
      public void init() throws ServletException {
        // String prefix = getServletContext().getRealPath("/");
        String file = getInitParameter("xml-config-file");
        File xmlConfigFile = new File(prefix + file);
        if (!xmlConfigFile.exists()) {
          System.out.println("attributes-config.xml not found, "
              + xmlConfigFile.getAbsolutePath());
        }
       
        try {
          // Configure Digester from XML ruleset
          AttributeBeanXmlConfigHelper.parse(xmlConfigFile);
       
          Logger.getLogger(this.getClass()).warn("Finished parsing the
config file.");
          is.close();
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
   }

What I thought I could do was something like this inside the try / catch:

InputStream is =
getServletContext().getResourceAsStream("WEB-INF/attributes-config.xml");

// Configure Digester from XML ruleset
AttributeBeanXmlConfigHelper.parse(xmlConfigFile);  

Now, the problem is... I think that there will be definitely be a
pathing issue for WEB-INF/attributes-config.xml when using
getResourceAsStream() (I am guessing because of my attempts with the
Log4jInitServlet postings). Also, how would one convert an InputStream
to a File?

e.g

How to convert "is" and "xmlConfigFile"?

Cheers,

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.