8

I have a growing web application which now needs to be able to store user and system preferences / settings. In the past I've always rolled my own preferences system for web applications but I was wondering what other people do to solve this problem? Are there any preference libraries for web applications that people could recommend?

Ideally user preferences should have a default value which the user can then override. Not all preferences should be exposed to the user though as some will be for things such as the last position of a dialog box.

If I go the roll-my-own route I think it will be a separate preferences table with all the preferences stored as strings converted to their true, primitive, data type as required. e.g. a table something like key,user_key,setting_name,setting_value. I favour this approach to a column per data type because it stops a setting from accidently ending up with two values and the consumer of a setting should know what data type they want.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
wobblycogs
  • 4,083
  • 7
  • 37
  • 48
  • Do you need to be able to change the prefs dynamically? Otherwise the JEE standard is the usage of `` which can be looked up or injected. – ewernli Jun 24 '10 at 12:07
  • Yes the preferences need to be dynamic. Some will be user set, for example their preferred theme, and others will be system set for the user, for example the last location of a dialog box. – wobblycogs Jun 24 '10 at 12:59

2 Answers2

3

One approach we use is:

  • all non-mandatory properties have defaults in code
  • deliver a properties file with the web application in which we define the technical oriented properties
  • query a SQL table on application startup to load mainly functional oriented properties

The properties from the database have a higher priority than those from the included property file. If your requirement is to prevent functional managers from changing techical properties you can add a context column to the properties table which is checked in the management UI.

All the application code gets to see is one globally used properties collection.

rsp
  • 23,135
  • 6
  • 55
  • 69
-3

There's a JDK API for that: java.util.prefs

https://docs.oracle.com/javase/8/docs/technotes/guides/preferences/index.html.

fospathi
  • 537
  • 1
  • 6
  • 7
Claude Vedovini
  • 2,421
  • 21
  • 19
  • 2
    Thanks, I was aware of the preference system and I've used it in a few desktop applications. It could probably be made to do what I want but I think it's a fairly poor choice for a web application. I see it's two biggest failings as no out of the box database storage of settings and no concept of user sessions (it supports per user settings but not in the manner a web application needs). – wobblycogs Jun 24 '10 at 17:38
  • Also the standard preferences implementation on UNIX uses a background thread to flush changes to the backing store and there's no way to stop this thread when reloading the webapp which leads to memory leaks. – Matthew Buckett Jan 11 '15 at 11:48