tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: Default folder in Tomcat
Date Sun, 29 Jul 2001 22:43:59 GMT


On Sun, 29 Jul 2001 mili@ibsplc.com wrote:

> Hello,
> Thank you for your replies. But I am not able to solve the problem.
> I would like to know the root folder (physical path in the server)  where
> I would have to put my properties file.
> 
> 

To use the Class.getResources() approach, your properties files go
wherever the classes that are reading them are, because the resources are
loaded by a class loader.  For a web application, that means you can put
the properties file into /WEB-INF/classes.  I would recommend a slight
variation of the approach shown below, because that approach will fail in
a web application that is *not* run from an unpacked directory.  Try this
instead (assuming your file is in /WEB-INF/classes):

  InputStream is =
   this.getClass().getClassLoader().getResourceAsStream("xyz.properties");
  Properties props = new Properties();
  props.load(is);
  is.close();

The above technique works in all Java programs.  Within the servlet API,
there is a similar mechanism that can load resources from within the web
app (for example, this is how the JSP page compiler actually loads the
source of your JSP pages so that they can be compiled).  For this example,
assume you've put your properties into the /WEB-INF directory (same place
as your web.xml file):

  InputStream is =
   getServletContext().getResourceAsStream("/WEB-INF/xyz.properties");
  Properties props = new Properties();
  props.load(is);
  is.close();

The above logic is assumed to be inside a servlet.

In both of the above cases, I used getResourceAsStream() instead of
getResource() because it is simpler.  You don't need the
getResource() version unless you really want to mess with URLs :-).

Craig McClanahan


> 
> 
> 
> 
> 
> djhutchison@imation.com
> 07/27/01 09:04 PM
> Please respond to tomcat-user
> 
>  
>         To:     tomcat-user@jakarta.apache.org
>         cc: 
>         Subject:        Re: Default folder in Tomcat
> 
> 
> 
> If you don't want to be tied to the servlet you could use the class loader
> by doing something like one of the following:
> 
> // Use this if you don't care where the file is located (just doing reads)
> this.getClass().getResourceAsStream("xyz.properties")
> 
> // Use this if need to know where the file is located (writing back to the 
> file)
> URL resUrl = 
> this.getClass().getClassLoader().getResource("xyz.properties");
> if (resUrl != null) {
>     Properties props = new Properties();
>     FileInputStream fs = new FileInputStream(resUrl.getFile());
>     props.load(fs);
> }
> 
> 
> 
> 
> 
> Ludovic Maitre <Ludovic.Maitre@sophia.inria.fr>@sophia.inria.fr on
> 07/27/2001 09:14:35 AM
> 
> Please respond to tomcat-user@jakarta.apache.org
> 
> Sent by:  Ludovic.Maitre@sophia.inria.fr
> 
> 
> To:   tomcat-user@jakarta.apache.org
> cc:
> 
> Subject:  Re: Default folder in Tomcat
> 
> 
> hello,
> 
> you can obtain the path where your application is located with the
> following function :
> WEBAPP_ROOT = getServletConfig().getServletContext().getRealPath()
> this is obtained from the sample file realpath.jsp, that i sugger you
> read.
> After obtaining the location of your webapp, simply add WEB-INF + the
> name of the property file.
> 
> mili@ibsplc.com wrote:
> >
> > Hello,
> >
> > I am using Tomcat for the first time, I have a doubt, can someone help
> > me with that please.
> >
> > I am refering to some properties files from my Bean.
> > I do not want to hardcode the path in my code.
> > Can someone plesae tell me the default folder
> > that the server reads from. I tried putting my files
> > in my webapps folder , it doesnt seem to work.
> >
> > Thanks in advance.
> > regs
> > M
> 
> --
>  Ludovic.Maitre@sophia.inria.fr
> 
>  INRIA - 2004 route des lucioles - BP 93    Tel: (33/0) 4 92 38 50 41
>  06902   SOPHIA-ANTIPOLIS cedex (France)    Fax: (33/0) 4 92 38 76 02
> 
> 
> 
> 
> 


Mime
View raw message