• 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

Application Scoped Managed Bean

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I intend to load some data from the database and loading it into the application Scope Context once during the lifetime of my application so it can be used by every managed Bean. I considered putting this code inside a managed bean marked as Application Scoped. But i realise that the Application Scoped Managed Bean does not execute unless an JSF action references a method contained in it, so in essence if nothing references that Application Scoped Bean. it might never get called...Suggestions are welcome to resolve this
[ October 23, 2007: Message edited by: Abiodun Adisa ]
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Create a basic servlet set up to initialize on startup.

2. Add an init() method to the servlet that constructs the bean and stores it at application scope.

3. Use the normal JSF processes to reference this bean as needed.

There's no difference between beans constructed and stored by JSF code and their counterparts constructed and stored by non-JSF code. All JSF is doing here is handling the construction for you (if you ask it to).
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way to achieve this is to use a ServetContextListener instead of managed bean creation facility. Use its contextInitialized method to put the variables in application scope.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please elaborate this ?
i mean i can track the context initilized by ServletContextListner but how can i initilize a manage bean with fully blown dependencies, and how can i put this to application scope
can you please elaborate with sample ..

thanks in advance.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lee,

you can register a class which implements ServletContextListener (for Servlet API 2.5 in your web.xml descriptor) which is called when a web application is started or the application context is reloaded for example with the Tomcat manager application.

You can store the application scoped bean or something as a Servlet context attribute. The listener will automatically receive a ServletContextEvent which in turn gives you access to context attributes. Where you get your application specific dependencies from for the object to create is of course up to you.

Just have a look to the Servlet API documentation for the said listener. If you still have problems to find out how it works feel free to ask

Marco
 
Lee Mark
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Macro,

web.xml



i am getting 'var' as null at deployment time,
how to inject properties that i dont understand,
can you please tell me,
Thanks again
!
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No no, this was not exactly what I described

You only USE the context listener to create an application scoped object but the listener class itself is not the application scoped object! You create the said object inside the listener and store it as a Servlet context attribute. Unfortunately I don't even know how exactly you can access scoped objects inside JSF 1.x (I guess it's 1.x) but I'm sure there's an easy way to do so.

Have a look at the Servlet API. The ServletContextEvent gives you access to the ServletContext which in turn has a method "setAttribute()" which can be used to store context (=application) wide objects which can then be accessed from insided JSP or JSF pages.

Marco
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic