You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mark Petrovic <ms...@gmail.com> on 2006/05/17 06:01:30 UTC

Application configuration: how to read files from a web application

A few people in the last few days on the Tomcat Users have essentially
asked: How do I read a file from disk from within my web application,
and furthermore, how do I declaratively configure the name of that file
so as to keep its path out of my code?

It's a common question, and there is a good solution that applies to
any Java application, not just a web application running in a container.

Here's the frequently used scheme:  bundle the file of interest in your
appplication's war file, and from within your application, read
its contents as a resource.

Assume the application's context is named myapp, the file of interest is
thisfile.dat, and that you can arrange for the file to be placed in
your application's WEB-INF/classes directory. When the application is
unbundled by Tomcat, you should see the file in a directory listing

$ cd $CATALINA_HOME
$ ls webapps/myapp/WEB-INF/classes/thisfile.dat
webapps/myapp/WEB-INF/classes/thisfile.dat

while this is clearly Unix-like syntax, the same ideas apply to Windows.

What have we done so far? Strategically place the file along the
application's classpath. This is essential to the scheme: the file
must be along the application's classpath - and nowhere else. This is
probably worth a close read

Class Loader HOW-TO:

http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html

Futhermore, to declaratively configure your application, you'd like to put
this file path in your application configuration, which keeps
it hardcoded out of your code. In other words, in the application's web.xmlfile.

So among other application configuration, you put this in your web.xml

<servlet>
   <init-param>
      <param-name>thefile</param-name>
      <param-value>/thisfile.dat</param-value>
   </init-param>
</servlet>

Finally, somewhere in your servlet, you set about retrieving the file
contents as follows:

String thefile = this.getInitParameter("thefile");
InputStream is = this.getClass().getResourceAsStream(thefile);

That's all there is to it. Of course, all of this assumes you have a method
of using an InputStream to parse or process the file, but that is generally
a good assumption.


-- 
Mark
AE6RT

RE: Application configuration: how to read files from a web application

Posted by Asaf Lahav <as...@primagrid.com>.
Well, Obviously I tried it.
The more interesting question is whether the default configuration tomcat
comes with permits writing files, and second is what should I look into at
our customers servers in order to make sure write/edit capabilities are
possible?

Asaf Lahav

VP R&D, Prima Grid LTD.

Cellular:  972-54-4717955

Phone:   972-3-6540255

Fax:       972-3-6540254

 

-----Original Message-----
From: Mark Petrovic [mailto:mspetrovic@gmail.com] 
Sent: Wednesday, May 17, 2006 11:31 AM
To: Tomcat Users List
Subject: Re: Application configuration: how to read files from a web
application

Asaf, hello.

Good question and I don't know of any best practices.  Others here may.

With the default security policy (running without "-security") I have not
attempted to write data from a web application, excluding of course
activities such as log messages or writing to sockets connected to a
database.  So my short answer is:  you'll just have  to try writing files to
see what happens.

Given that, you cannot or do not want to be in the business of attempting to
modify the war file that held the original configuration.  Obvious enough,
but probably needs to be said.  You also want to consider a saved-config
file location in the file system that does not get overwritten when new war
files are deployed, meaning someplace above WEB-INF/ (I believe that is
possible).

Wish I could help more - I just don't know first hand any more to tell.  I'd
start with

FileOutputStream fos = new FileOutputStream("configfile");
fos.write("bytes".getBytes());
fos.close();

and see what happens.

Mark

On 5/17/06, Asaf Lahav <as...@primagrid.com> wrote:
>
> Hi Mark,
>
> Is there a best practice for also writing/updating configuration file from
> within the application?
>
> Asaf Lahav
>
> VP R&D, Prima Grid LTD.
>
> Cellular:  972-54-4717955
>
> Phone:   972-3-6540255
>
> Fax:       972-3-6540254
>
>
>
>
> -----Original Message-----
> From: Mark Petrovic [mailto:mspetrovic@gmail.com]
> Sent: Wednesday, May 17, 2006 6:02 AM
> To: Tomcat Users List
> Subject: Application configuration: how to read files from a web
> application
>
> A few people in the last few days on the Tomcat Users have essentially
> asked: How do I read a file from disk from within my web application,
> and furthermore, how do I declaratively configure the name of that file
> so as to keep its path out of my code?
>
> It's a common question, and there is a good solution that applies to
> any Java application, not just a web application running in a container.
>
> Here's the frequently used scheme:  bundle the file of interest in your
> appplication's war file, and from within your application, read
> its contents as a resource.
>
> Assume the application's context is named myapp, the file of interest is
> thisfile.dat, and that you can arrange for the file to be placed in
> your application's WEB-INF/classes directory. When the application is
> unbundled by Tomcat, you should see the file in a directory listing
>
> $ cd $CATALINA_HOME
> $ ls webapps/myapp/WEB-INF/classes/thisfile.dat
> webapps/myapp/WEB-INF/classes/thisfile.dat
>
> while this is clearly Unix-like syntax, the same ideas apply to Windows.
>
> What have we done so far? Strategically place the file along the
> application's classpath. This is essential to the scheme: the file
> must be along the application's classpath - and nowhere else. This is
> probably worth a close read
>
> Class Loader HOW-TO:
>
> http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html
>
> Futhermore, to declaratively configure your application, you'd like to put
> this file path in your application configuration, which keeps
> it hardcoded out of your code. In other words, in the application's
> web.xmlfile.
>
> So among other application configuration, you put this in your web.xml
>
> <servlet>
>    <init-param>
>       <param-name>thefile</param-name>
>       <param-value>/thisfile.dat</param-value>
>    </init-param>
> </servlet>
>
> Finally, somewhere in your servlet, you set about retrieving the file
> contents as follows:
>
> String thefile = this.getInitParameter("thefile");
> InputStream is = this.getClass().getResourceAsStream(thefile);
>
> That's all there is to it. Of course, all of this assumes you have a
> method
> of using an InputStream to parse or process the file, but that is
> generally
> a good assumption.
>
>
> --
> Mark
> AE6RT
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
Mark
AE6RT


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Application configuration: how to read files from a web application

Posted by Mark Petrovic <ms...@gmail.com>.
Asaf, hello.

Good question and I don't know of any best practices.  Others here may.

With the default security policy (running without "-security") I have not
attempted to write data from a web application, excluding of course
activities such as log messages or writing to sockets connected to a
database.  So my short answer is:  you'll just have  to try writing files to
see what happens.

Given that, you cannot or do not want to be in the business of attempting to
modify the war file that held the original configuration.  Obvious enough,
but probably needs to be said.  You also want to consider a saved-config
file location in the file system that does not get overwritten when new war
files are deployed, meaning someplace above WEB-INF/ (I believe that is
possible).

Wish I could help more - I just don't know first hand any more to tell.  I'd
start with

FileOutputStream fos = new FileOutputStream("configfile");
fos.write("bytes".getBytes());
fos.close();

and see what happens.

Mark

On 5/17/06, Asaf Lahav <as...@primagrid.com> wrote:
>
> Hi Mark,
>
> Is there a best practice for also writing/updating configuration file from
> within the application?
>
> Asaf Lahav
>
> VP R&D, Prima Grid LTD.
>
> Cellular:  972-54-4717955
>
> Phone:   972-3-6540255
>
> Fax:       972-3-6540254
>
>
>
>
> -----Original Message-----
> From: Mark Petrovic [mailto:mspetrovic@gmail.com]
> Sent: Wednesday, May 17, 2006 6:02 AM
> To: Tomcat Users List
> Subject: Application configuration: how to read files from a web
> application
>
> A few people in the last few days on the Tomcat Users have essentially
> asked: How do I read a file from disk from within my web application,
> and furthermore, how do I declaratively configure the name of that file
> so as to keep its path out of my code?
>
> It's a common question, and there is a good solution that applies to
> any Java application, not just a web application running in a container.
>
> Here's the frequently used scheme:  bundle the file of interest in your
> appplication's war file, and from within your application, read
> its contents as a resource.
>
> Assume the application's context is named myapp, the file of interest is
> thisfile.dat, and that you can arrange for the file to be placed in
> your application's WEB-INF/classes directory. When the application is
> unbundled by Tomcat, you should see the file in a directory listing
>
> $ cd $CATALINA_HOME
> $ ls webapps/myapp/WEB-INF/classes/thisfile.dat
> webapps/myapp/WEB-INF/classes/thisfile.dat
>
> while this is clearly Unix-like syntax, the same ideas apply to Windows.
>
> What have we done so far? Strategically place the file along the
> application's classpath. This is essential to the scheme: the file
> must be along the application's classpath - and nowhere else. This is
> probably worth a close read
>
> Class Loader HOW-TO:
>
> http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html
>
> Futhermore, to declaratively configure your application, you'd like to put
> this file path in your application configuration, which keeps
> it hardcoded out of your code. In other words, in the application's
> web.xmlfile.
>
> So among other application configuration, you put this in your web.xml
>
> <servlet>
>    <init-param>
>       <param-name>thefile</param-name>
>       <param-value>/thisfile.dat</param-value>
>    </init-param>
> </servlet>
>
> Finally, somewhere in your servlet, you set about retrieving the file
> contents as follows:
>
> String thefile = this.getInitParameter("thefile");
> InputStream is = this.getClass().getResourceAsStream(thefile);
>
> That's all there is to it. Of course, all of this assumes you have a
> method
> of using an InputStream to parse or process the file, but that is
> generally
> a good assumption.
>
>
> --
> Mark
> AE6RT
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
Mark
AE6RT

Re: Application configuration: how to read files from a web application

Posted by Hans Sowa <ha...@gmail.com>.
Hi

Download the log4j Source from Apache. Look at the class
org.apache.log4j.helpers.Loader and here you will a logic which is used to
find the log4j.xml. This works for every application.

Hope this will help.

On 5/17/06, Tim Lucia <ti...@yahoo.com> wrote:
>
> I have always responded with this
>
> http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html
>
> when this question comes up.  It explains the most common ways to find and
> load resource files.  It does not get specifically into servlets, however,
> or ServletContext.getResource[AsStream](path), which is worth knowing
> about.
>
> On the subject of writing configuration, I have used a database to store
> modifiable configuration settings (those changeable from within the app
> itself, such as preferences.)  Otherwise, I have relied on modifying xml
> configuration files and/or using the Tomcat admin application.
>
> Tim
>
>
>
> -----Original Message-----
> From: Mark Petrovic [mailto:mspetrovic@gmail.com]
> Sent: Wednesday, May 17, 2006 12:02 AM
> To: Tomcat Users List
> Subject: Application configuration: how to read files from a web
> application
>
> A few people in the last few days on the Tomcat Users have essentially
> asked: How do I read a file from disk from within my web application,
> and furthermore, how do I declaratively configure the name of that file
> so as to keep its path out of my code?
>
> It's a common question, and there is a good solution that applies to
> any Java application, not just a web application running in a container.
>
> Here's the frequently used scheme:  bundle the file of interest in your
> appplication's war file, and from within your application, read
> its contents as a resource.
>
> Assume the application's context is named myapp, the file of interest is
> thisfile.dat, and that you can arrange for the file to be placed in
> your application's WEB-INF/classes directory. When the application is
> unbundled by Tomcat, you should see the file in a directory listing
>
> $ cd $CATALINA_HOME
> $ ls webapps/myapp/WEB-INF/classes/thisfile.dat
> webapps/myapp/WEB-INF/classes/thisfile.dat
>
> while this is clearly Unix-like syntax, the same ideas apply to Windows.
>
> What have we done so far? Strategically place the file along the
> application's classpath. This is essential to the scheme: the file
> must be along the application's classpath - and nowhere else. This is
> probably worth a close read
>
> Class Loader HOW-TO:
>
> http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html
>
> Futhermore, to declaratively configure your application, you'd like to put
> this file path in your application configuration, which keeps
> it hardcoded out of your code. In other words, in the application's
> web.xmlfile.
>
> So among other application configuration, you put this in your web.xml
>
> <servlet>
>    <init-param>
>       <param-name>thefile</param-name>
>       <param-value>/thisfile.dat</param-value>
>    </init-param>
> </servlet>
>
> Finally, somewhere in your servlet, you set about retrieving the file
> contents as follows:
>
> String thefile = this.getInitParameter("thefile");
> InputStream is = this.getClass().getResourceAsStream(thefile);
>
> That's all there is to it. Of course, all of this assumes you have a
> method
> of using an InputStream to parse or process the file, but that is
> generally
> a good assumption.
>
>
> --
> Mark
> AE6RT
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
mfg Hans Sowa
mailto:hanssowa@gmail.com

RE: Application configuration: how to read files from a web application

Posted by Asaf Lahav <as...@primagrid.com>.
Hi Mark,

Is there a best practice for also writing/updating configuration file from
within the application?

Asaf Lahav

VP R&D, Prima Grid LTD.

Cellular:  972-54-4717955

Phone:   972-3-6540255

Fax:       972-3-6540254

 


-----Original Message-----
From: Mark Petrovic [mailto:mspetrovic@gmail.com] 
Sent: Wednesday, May 17, 2006 6:02 AM
To: Tomcat Users List
Subject: Application configuration: how to read files from a web application

A few people in the last few days on the Tomcat Users have essentially
asked: How do I read a file from disk from within my web application,
and furthermore, how do I declaratively configure the name of that file
so as to keep its path out of my code?

It's a common question, and there is a good solution that applies to
any Java application, not just a web application running in a container.

Here's the frequently used scheme:  bundle the file of interest in your
appplication's war file, and from within your application, read
its contents as a resource.

Assume the application's context is named myapp, the file of interest is
thisfile.dat, and that you can arrange for the file to be placed in
your application's WEB-INF/classes directory. When the application is
unbundled by Tomcat, you should see the file in a directory listing

$ cd $CATALINA_HOME
$ ls webapps/myapp/WEB-INF/classes/thisfile.dat
webapps/myapp/WEB-INF/classes/thisfile.dat

while this is clearly Unix-like syntax, the same ideas apply to Windows.

What have we done so far? Strategically place the file along the
application's classpath. This is essential to the scheme: the file
must be along the application's classpath - and nowhere else. This is
probably worth a close read

Class Loader HOW-TO:

http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html

Futhermore, to declaratively configure your application, you'd like to put
this file path in your application configuration, which keeps
it hardcoded out of your code. In other words, in the application's
web.xmlfile.

So among other application configuration, you put this in your web.xml

<servlet>
   <init-param>
      <param-name>thefile</param-name>
      <param-value>/thisfile.dat</param-value>
   </init-param>
</servlet>

Finally, somewhere in your servlet, you set about retrieving the file
contents as follows:

String thefile = this.getInitParameter("thefile");
InputStream is = this.getClass().getResourceAsStream(thefile);

That's all there is to it. Of course, all of this assumes you have a method
of using an InputStream to parse or process the file, but that is generally
a good assumption.


-- 
Mark
AE6RT


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Application configuration: how to read files from a web application

Posted by Tim Lucia <ti...@yahoo.com>.
I have always responded with this

http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html

when this question comes up.  It explains the most common ways to find and
load resource files.  It does not get specifically into servlets, however,
or ServletContext.getResource[AsStream](path), which is worth knowing about.

On the subject of writing configuration, I have used a database to store
modifiable configuration settings (those changeable from within the app
itself, such as preferences.)  Otherwise, I have relied on modifying xml
configuration files and/or using the Tomcat admin application.

Tim



-----Original Message-----
From: Mark Petrovic [mailto:mspetrovic@gmail.com] 
Sent: Wednesday, May 17, 2006 12:02 AM
To: Tomcat Users List
Subject: Application configuration: how to read files from a web application

A few people in the last few days on the Tomcat Users have essentially
asked: How do I read a file from disk from within my web application,
and furthermore, how do I declaratively configure the name of that file
so as to keep its path out of my code?

It's a common question, and there is a good solution that applies to
any Java application, not just a web application running in a container.

Here's the frequently used scheme:  bundle the file of interest in your
appplication's war file, and from within your application, read
its contents as a resource.

Assume the application's context is named myapp, the file of interest is
thisfile.dat, and that you can arrange for the file to be placed in
your application's WEB-INF/classes directory. When the application is
unbundled by Tomcat, you should see the file in a directory listing

$ cd $CATALINA_HOME
$ ls webapps/myapp/WEB-INF/classes/thisfile.dat
webapps/myapp/WEB-INF/classes/thisfile.dat

while this is clearly Unix-like syntax, the same ideas apply to Windows.

What have we done so far? Strategically place the file along the
application's classpath. This is essential to the scheme: the file
must be along the application's classpath - and nowhere else. This is
probably worth a close read

Class Loader HOW-TO:

http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html

Futhermore, to declaratively configure your application, you'd like to put
this file path in your application configuration, which keeps
it hardcoded out of your code. In other words, in the application's
web.xmlfile.

So among other application configuration, you put this in your web.xml

<servlet>
   <init-param>
      <param-name>thefile</param-name>
      <param-value>/thisfile.dat</param-value>
   </init-param>
</servlet>

Finally, somewhere in your servlet, you set about retrieving the file
contents as follows:

String thefile = this.getInitParameter("thefile");
InputStream is = this.getClass().getResourceAsStream(thefile);

That's all there is to it. Of course, all of this assumes you have a method
of using an InputStream to parse or process the file, but that is generally
a good assumption.


-- 
Mark
AE6RT


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org