Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Global config variables in a web application - programming style?  RSS feed

 
Keimo Smith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm looking for general advice as to how I should implement global config variables in a webapp.

At the moment I create a singleton object at startup (done in ServletContextListener.contextInitialized()...). My singleton reads a config file and stores the values in variables. I used a singleton to make sure that there are no multiple copies of the global config variables.




I did not manage to code a singleton java bean so my singleton class object is just an attribute stored in ServletContext. Is it possible/sensible to create a java bean as a singleton class?


Now I see 2 alternatives:

1)
Use scripting to get the values out of the singleton e.g:



The problem with this is, I understand one should use Expression Language today and not scriptlets?


2)
Create a normal java bean and store it in ServletContext. This approach would rely on the principle that nobody creates new instances of the bean. This would not be a problem in our organisation but somehow this solution feels like a compromise to me.





Or am I completely lost here. Is there a common way how this should be done. None of my books really address this and I have not been able to find much on the internet. Is the simple java bean approach an accepted solution or would it be considered bad style? For our new application I would like to get this basic principle right from the start.

Cheers
Keimo

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really understand the difference between your two scenarios except that in the first you use discredited scriptlets and in the second you use the EL.

In any case, a bean in the application context is the way I almost always handle this. The bean is usually initialized and placed into application scope by a context listener that executes when the app starts up.
 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha! Intresting, If we have some "Global config variables" in server start up what type it would be - an Object or just a string? Somehow if you have a xml parsed and created an Object (if it is only on start up or may be even not here ) it makes sence to have a singleton else i do not see any reason for going to singleton pattern for simple String types. I prefer a static variable instead, any contradictions ?
 
Keimo Smith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your advice. It clarified this a great deal.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!