You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by C F <ta...@yahoo.com> on 2002/09/22 09:07:17 UTC

How to manage application properties?

Hello,

This is a very "newbie" question I'm sure, so this might not be the appropriate forum.  Maybe it belongs in the Tomcat forum?  Not sure.

Pretty basic objective.... I just want to be able to put application settings (things like path names, integer values, etc) in a *.properties file and access those properties from within my tomcat/struts(1.1) application.  I see quite a bit of talk about ApplicationResources.properties, but it seems like people are only using that to store and retrieve messages.  I'd rather not mix my messages with my settings.  How do you do it?  I'm aware of the java.util.properties class.... but I don't really know how to efficiently use it within the application servlet context (I don't want to reload it with every request)..... and I wouldn't know when to load it into application scope.....   anyway, you see that I don't have a clue.  I can think of plenty of ways to do it, but I would like know the most common/accepted method(s).  So any tips would be much appreciated :)  

Thanks!

 



---------------------------------
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!

Re: How to manage application properties?

Posted by C F <ta...@yahoo.com>.
Excellent... thanks for the info.  I probably average 5-10 properties for some of my controller servlets, so I think I'll use the init-param method as you suggested.  I guess I just got confused as to why there was the ApplicationResources.properties files when we did have the init-param/servlet option available to us..... so I wasn't really sure which path I should go down.  I guess that properties file is there because people will potentially have huge numbers of message resources and also enables the internationalization aspect.  Appreciate the help!
 
 Eddie Bush wrote:How many "settings" do you have? Are they volatile?

You could:
- specify settings as an init-param to the controller servlet.
- specify settings as context params
- use a properties file (as you mentioned) and look it up out of the 
classpath

To get your properties read-in, you could:
- write an initialization servlet
- write a context listener
- write a struts plugin

All that your initializer (any of them) would have to do is something like:

java.io.InputStream is = 
.getServletContext().getResourceAsStream(configFile);

The "someObject" would vary depending on which approach you took. There 
are methods on the Properties class to read from an InputStream. See 
javadoc ;-)

In order to store your newly loaded properties into application scope 
(if they're not particular to a certain user, this is where you should 
put them), you'd simply do something like:

.getServletContext().setAttribute("mySpecialProps", 
mySpecialProps);

Where:
- someObject varies by your implementation again
- "mySpecialProps" is the key you wish to use for looking up the 
properties
- mySpecialProps is an instance of Properties (the one you just loaded)

C F wrote:

>Hello,
>
>This is a very "newbie" question I'm sure, so this might not be the appropriate forum. Maybe it belongs in the Tomcat forum? Not sure.
>
>Pretty basic objective.... I just want to be able to put application settings (things like path names, integer values, etc) in a *.properties file and access those properties from within my tomcat/struts(1.1) application. I see quite a bit of talk about ApplicationResources.properties, but it seems like people are only using that to store and retrieve messages. I'd rather not mix my messages with my settings. How do you do it? I'm aware of the java.util.properties class.... but I don't really know how to efficiently use it within the application servlet context (I don't want to reload it with every request)..... and I wouldn't know when to load it into application scope..... anyway, you see that I don't have a clue. I can think of plenty of ways to do it, but I would like know the most common/accepted method(s). So any tips would be much appreciated :) 
>
>Thanks!
>

-- 
Eddie Bush




--
To unsubscribe, e-mail: 
For additional commands, e-mail: 



---------------------------------
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!

Re: How to manage application properties?

Posted by Eddie Bush <ek...@swbell.net>.
How many "settings" do you have?  Are they volatile?

You could:
    - specify settings as an init-param to the controller servlet.
    - specify settings as context params
    - use a properties file (as you mentioned) and look it up out of the 
classpath

To get your properties read-in, you could:
    - write an initialization servlet
    - write a context listener
    - write a struts plugin

All that your initializer (any of them) would have to do is something like:

    java.io.InputStream is = 
<someObject>.getServletContext().getResourceAsStream(configFile);

The "someObject" would vary depending on which approach you took.  There 
are methods on the Properties class to read from an InputStream.  See 
javadoc ;-)

In order to store your newly loaded properties into application scope 
(if they're not particular to a certain user, this is where you should 
put them), you'd simply do something like:

    <someObject>.getServletContext().setAttribute("mySpecialProps", 
mySpecialProps);

Where:
    - someObject varies by your implementation again
    - "mySpecialProps" is the key you wish to use for looking up the 
properties
    - mySpecialProps is an instance of Properties (the one you just loaded)

C F wrote:

>Hello,
>
>This is a very "newbie" question I'm sure, so this might not be the appropriate forum.  Maybe it belongs in the Tomcat forum?  Not sure.
>
>Pretty basic objective.... I just want to be able to put application settings (things like path names, integer values, etc) in a *.properties file and access those properties from within my tomcat/struts(1.1) application.  I see quite a bit of talk about ApplicationResources.properties, but it seems like people are only using that to store and retrieve messages.  I'd rather not mix my messages with my settings.  How do you do it?  I'm aware of the java.util.properties class.... but I don't really know how to efficiently use it within the application servlet context (I don't want to reload it with every request)..... and I wouldn't know when to load it into application scope.....   anyway, you see that I don't have a clue.  I can think of plenty of ways to do it, but I would like know the most common/accepted method(s).  So any tips would be much appreciated :)  
>
>Thanks!
>

-- 
Eddie Bush




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: How to manage application properties?

Posted by kiuma <ki...@usa.net>.
You should prefer to use struts-config.xml to get applets props.
Take a look to the samples bundled with struts and also use struts 
console to configure struts, at least at the beginning!


C F wrote:

>Hello,
>
>This is a very "newbie" question I'm sure, so this might not be the appropriate forum.  Maybe it belongs in the Tomcat forum?  Not sure.
>
>Pretty basic objective.... I just want to be able to put application settings (things like path names, integer values, etc) in a *.properties file and access those properties from within my tomcat/struts(1.1) application.  I see quite a bit of talk about ApplicationResources.properties, but it seems like people are only using that to store and retrieve messages.  I'd rather not mix my messages with my settings.  How do you do it?  I'm aware of the java.util.properties class.... but I don't really know how to efficiently use it within the application servlet context (I don't want to reload it with every request)..... and I wouldn't know when to load it into application scope.....   anyway, you see that I don't have a clue.  I can think of plenty of ways to do it, but I would like know the most common/accepted method(s).  So any tips would be much appreciated :)  
>
>Thanks!
>
> 
>
>
>
>---------------------------------
>Do you Yahoo!?
>New DSL Internet Access from SBC & Yahoo!
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: How to manage application properties?

Posted by micael <ca...@harbornet.com>.
You can create classes and objects in struts just like you can with any 
other application.  Attached is a file with a few classes I use to teach 
students about the Properties class in java.util.  Change the value in 
Folder (line 138, if you are not using Windows).  PropertiesManager creates 
properties files from which the locations of other properties files is 
managed.  It also uses default properties, etc.

Micael

At 12:07 AM 9/22/2002 -0700, you wrote:

>Hello,
>
>This is a very "newbie" question I'm sure, so this might not be the 
>appropriate forum.  Maybe it belongs in the Tomcat forum?  Not sure.
>
>Pretty basic objective.... I just want to be able to put application 
>settings (things like path names, integer values, etc) in a *.properties 
>file and access those properties from within my tomcat/struts(1.1) 
>application.  I see quite a bit of talk about 
>ApplicationResources.properties, but it seems like people are only using 
>that to store and retrieve messages.  I'd rather not mix my messages with 
>my settings.  How do you do it?  I'm aware of the java.util.properties 
>class.... but I don't really know how to efficiently use it within the 
>application servlet context (I don't want to reload it with every 
>request)..... and I wouldn't know when to load it into application 
>scope.....   anyway, you see that I don't have a clue.  I can think of 
>plenty of ways to do it, but I would like know the most common/accepted 
>method(s).  So any tips would be much appreciated :)
>
>Thanks!
>
>
>
>
>
>---------------------------------
>Do you Yahoo!?
>New DSL Internet Access from SBC & Yahoo!

Re: How to manage application properties?

Posted by micael <ca...@harbornet.com>.
Attached is a demo.Demo class which utilizes a PropertiesManager 
class.  The PropertiesManager class creates a properties file to keep track 
of properties files, then creates and uses properties files with and 
without defaults.  I use it to demonstrate to students.

You can put anything you like into any scope (page, session, application) 
in an application, so you use property files just like you would with any 
program or application.  There is no reason why you cannot create and use 
classes and objects in struts.
This PropertiesManager is probably too unwieldy, but you can make something 
that fits your own needs.

Micael

At 12:07 AM 9/22/2002 -0700, you wrote:

>Hello,
>
>This is a very "newbie" question I'm sure, so this might not be the 
>appropriate forum.  Maybe it belongs in the Tomcat forum?  Not sure.
>
>Pretty basic objective.... I just want to be able to put application 
>settings (things like path names, integer values, etc) in a *.properties 
>file and access those properties from within my tomcat/struts(1.1) 
>application.  I see quite a bit of talk about 
>ApplicationResources.properties, but it seems like people are only using 
>that to store and retrieve messages.  I'd rather not mix my messages with 
>my settings.  How do you do it?  I'm aware of the java.util.properties 
>class.... but I don't really know how to efficiently use it within the 
>application servlet context (I don't want to reload it with every 
>request)..... and I wouldn't know when to load it into application 
>scope.....   anyway, you see that I don't have a clue.  I can think of 
>plenty of ways to do it, but I would like know the most common/accepted 
>method(s).  So any tips would be much appreciated :)
>
>Thanks!
>
>
>
>
>
>---------------------------------
>Do you Yahoo!?
>New DSL Internet Access from SBC & Yahoo!