Procedure: Using properties files in Java

1 About this document

Purpose
This document outlines a few unofficial steps on implementing properties files in a Java web application using Eclipse as the IDE. In this case we are on a Unix based OS.
Audience
Java developers using Eclipse
Requirements
Eclipse IDE
This version date

02/21/09 09:55:26 AM

2 Create a properties file and save it in your Classpath

user.name = tim
user.password = mypassword
more.stuff = thisandthat

3 Code for the Servlet to initialize the properties class

String userPropertiesFile = /this_file.txt;
Properties prop = new Properties();
    {
        try {
            prop.load((this.getClass()
                    .getResourceAsStream(this.userPropertiesFile)));
        } catch (Exception e) {
            System.out.println("Unable to load Properties file" + e);
        }
    };

4 Code for servlet to obtain values

String password = prop.getProperty("user.password", "defaultValue"));
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s