title: Loading properties file in a Java Jersey RESTful web app, to persist throughout the app?
body: <p>I'm currently building a RESTful API using Jersey. So far, all has been going well, however, all of the configuration entries have been hard coded in. (i.e. <strong>Database Host</strong>, <strong>Database Username</strong>, etc...). </p>
  
  <p>I'd like to be able to setup a <code>config.properties</code> file that exists in my <code>WEB-INF</code> folder to contain all of these configuration specs. </p>
  
  <p>I'm concerned that if I do it the "classic" way of reading the file on the Classpath, I'm performing file I/O for every request. I want to be able to read once on startup (which I know involves a <code>ServletListener</code> in my <code>web.xml</code> file. </p>
  
  <p>Here's what I have below:</p>
  
  <p><strong>web.xml</strong>:</p>
  
  <pre><code>&lt;listener&gt;
      &lt;listener-class&gt;com._1834Software.Config&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p>I'd like to do something like this (which I found <a href="https://stackoverflow.com/questions/3153739/config-files-for-a-webapplication-load-once-and-store-where">here</a> on StackOverflow) but I don't think it works necessarily with Jersey:</p>
  
  <p><strong>Config.java</strong></p>
  
  <pre><code>public class Config implements ServletContextListener {
      private static final String ATTRIBUTE_NAME = "config";
      private Properties config = new Properties();
  
      @Override
      public void contextInitialized(ServletContextEvent event) {
          try {
  
              config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
  
          } catch (IOException err) {
              err.printStackTrace();
          }
  
          event.getServletContext().setAttribute(ATTRIBUTE_NAME, this);
  
      }
  
      @Override
      public void contextDestroyed(ServletContextEvent event) { /**/ }
  
      public static Config getInstance(ServletContext context) {
          return (Config) context.getAttribute(ATTRIBUTE_NAME);
      }
  
      public String getProperty(String key) {
          return config.getProperty(key);
      }
  
  }
  </code></pre>
  
  <p>I try to call it like such:</p>
  
  <pre><code>Config config = Config.getInstance(getServletContext());
  String property = config.getProperty("HEROKU_DATABASE_URL");
  </code></pre>
  
  <p>But I get the following error:</p>
  
  <pre><code>Error:(32, 40) java: cannot find symbol
  symbol:   method getServletContext()
  location: class com._1834Software.database.DatabaseHandler
  </code></pre>
  
  <p>And here is the file (<code>DatabaseHandler.java</code> where I'm trying to call it):</p>
  
  <pre><code>public class DatabaseHandler {
      public Connection connection = null;
  
      Config config = Config.getInstance(getServletContext());
      String property = config.getProperty("somekey");
  
  
      /* Database Parameters */
      private String DRIVER = "org.postgresql.Driver";
      private String host = "XXXXX";
      private String userName = "XXXXX";
      private String password = "XXXXX";
  
      public void connect() throws SQLException {
          try {
  
              Class.forName(DRIVER);
  
          } catch (ClassNotFoundException err) {
  
              err.printStackTrace();   
  
          }
  
          try {
  
              connection = DriverManager.getConnection(host, userName, password);
  
          } catch (SQLException err) {
              err.printStackTrace();
          }
      }
  
      public void disconnect() throws SQLException { connection.close(); }
  }
  </code></pre>