Hello

Here is some stuff that works for me when it comes to *loading* (reading)
properties from (usually) a servlet init method...


    public void init( ServletConfig config ) throws ServletException {

        super.init( config );

        String dbDriver = null;
        String dbURL    = null;
        String username = null;
        String password = null;

        Properties props = new Properties();
        ServletContext context = config.getServletContext();
        String path = "/WEB-INF/classes/props/my_props.txt";

        InputStream in = context.getResourceAsStream( path );
        if( in != null ) {
            try {
                props.load( in );
                dbDriver = props.getProperty( "dbDriver", "null" );
                dbURL    = props.getProperty( "dbURL",    "null" );
                username = props.getProperty( "username", "null" );
                password = props.getProperty( "password", "null" );
                ... // Read other values.
            } catch ( IOException e ) {
                ... // Set default values or handle exception.
            } finally {
                try { in.close(); } catch ( IOException ignore ) {}
            }
        } else { 
            ... // Set default values.
        }
        
    } // method


The 'my_props.txt' properties file would contain the following name-value
pairs:

    dbDriver = oracle.jdbc.driver.OracleDriver
    dbURL    = jdbc:oracle:thin:@123.123.123.123:1521:xyz
    username = myusername
    password = mypassword

The API for the 'java.util.Properties.load' method has a full description
how to write a suitable property file.

It would be easy enough to write out a property file - from (say) a servlet
destroy method - which could be loaded as above.

You are probably right about using the temp directory provided by the
ServletContext: it is the only way to *write* stuff that I can think of, but
beware that the specification does not say anything about temp files being
saved between invocations!

Good luck.

Harry Mantheakis
London


> thanks, but the whole point of this is to persist a property across restarts -
> whether they be the entire VM or just the web-app.
> 
> Is there a consistent wat to persist property information for the web-app to
> the filesystem, in the same way that sessions can be
> persisted ?
> I know that the servlet spec requires the ServletContext to provide a tempdir,
> so I will probably try serializing my properties to a
> file in that dir.
> 
> 
> 
> 
>> -----Original Message-----
>> From: Jacob Kjome [mailto:[EMAIL PROTECTED]
>> Sent: Friday, November 07, 2003 2:17 PM
>> To: Tomcat Users List
>> Subject: Re: How to persist web-app properties across restarts ?
>> 
>> 
>> 
>> Put a singleton helper class in a parent classloader such as in shared or
>> common Tomcat classloaders and manipulate collections there.  They will
>> exist as long as the JVM (and Tomcat) is running.
>> 
>> Otherwise, you should also be able to use stuff like System.setProperty().
>> 
>> Jake
>> 
>> At 01:51 PM 11/7/2003 -0500, you wrote:
>> 
>>> I am looking for the most standard and lightweight way to persist some
>>> properties about a web application across restarts of the web
>>> application or the entire server.
>>> Something like the ServletContext.setAttribute() method, but with the
>>> persistence ability that you get from Http sessions would be
>>> perfect.
>>> I am using this for a management servlet that can indicate to load
>>> balancing software that traffic needs to be directed away from a
>>> given tomcat instance when it's in a certain state, and the management
>>> servlet needs to remember what state it was last in when it's
>>> application is restarted. Because it's used in monitoring I don't want any
>>> dependancies on external data stores other than simple
>>> file system.
>>> 
>>> Thanks
>>> 
>>> Steph


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to