• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Global config variables in a web application - programming style?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 329
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your advice. It clarified this a great deal.
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic