ResourceBundles vs. Loading Properties

I have an Initialization Servlet I've used for quite a while now, it loads up when the web-app starts and grabs a .properties file with the arbitrary options I need to store for the application - everything from media directories to web service passwords.

Here's what it looks like:

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class InitializationServlet extends HttpServlet {

        private final static String SITE_CONFIG="config";

        public void init(ServletConfig servletConfig) throws ServletException {


                String configPath = servletConfig.getInitParameter(SITE_CONFIG);
                if("".equals(configPath) || configPath == null){
                        throw new ServletException("Config param not found");

                Properties configurationProperties = new Properties();
                InputStream is = servletConfig.getServletContext().getResourceAsStream(configPath);
                try {

                        Iterator keyIterator = configurationProperties.keySet().iterator();
                        while (keyIterator.hasNext()) {
                                String key = (String);
                                String propertyValue = configurationProperties.getProperty(key);
                                servletConfig.getServletContext().log(key + " - " + propertyValue);
                                System.out.println(key + " - " + propertyValue);

                        servletConfig.getServletContext().setAttribute("config", config);


                } catch (Exception e) {
                        throw new ServletException("Error loading config.", e);



But then I noticed on Erik's Linkblog a link to this post about ResourceBundles in Servlets by Eddy in Mauritius. In it he gives this bit of example code:

ResourceBundle rb = ResourceBundle.getBundle("messages", Locale.ENGLISH, getClass().getClassLoader());

Umm. Wow. That's a hell of a lot cleaner, no? And I don't need a dedicated Servlet to pull in those values. A ResourceBundle isn't a Properties class by any stretch, but for what I need it's pretty much perfect. I've played with this before but could never get it working (the ClassLoader() got me), so this is an amazing discovery.

The question is, which is the best thing to do? The ResourceBundle properties file needs to be in the CLASSPATH, which may be a GoodThing (TM) as you could dynamically swap out properties just by changing an environment variable, but is a potential source of bugs as versions pop up in .jar files you weren't expecting. The other thing is that you can't save values to a ResourceBundle - it's read only. Though I can promise you, I've never even thought about doing that in a webapp.

Or maybe there's another option? Personally the whole ResourceBundle vs Properties vs. PropertyResourceBundle vs JDK 1.4's java.util.prefs package just boggles me. One line for pulling in default property values via the ResourceBundle is mighty tempting though, I have to say, regardless of whether it's the "right" thing to do or not.


P.S. rules! It's quickly become as frequently used while I program as Google. That's how I grabbed the urls to the classes above. Seriously, Pete Freitag (the guy who launched that site) deserves a medal, a donation, and a free pass to JavaOne for life.

< Previous         Next >