Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 91041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:58:04+00:00 2026-05-10T22:58:04+00:00

I’m building a JSF+Facelets web app, one piece of which is a method that

  • 0

I’m building a JSF+Facelets web app, one piece of which is a method that scans a directory every so often and indexes any changes. This method is part of a bean which is in application scope. I have built a subclass of TimerTask to call the method every X milliseconds. My problem is getting the bean initialized. I can reference the bean on a page, and when I go to the page, the bean is initialized, and works as directed; what I would like instead is for the bean to be initialized when the web context is initialized, so that it doesn’t require a page visit to start the indexing method. Google has shown a few people that want this functionality, but no real solutions outside of integrating with Spring, which I really don’t want to do just to get this piece of functionality.

I’ve tried playing around with both the servlets that have ‘load-on-startup’ set, and a ServletContextListener to get things going, and haven’t been able to get the set up right, either because there isn’t a FacesContext available, or because I can’t reference the bean from the JSF environment.

Is there any way to get a JSF bean initialized on web app startup?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:58:05+00:00Added an answer on May 10, 2026 at 10:58 pm

    If your code calls FacesContext, it will not work outside a thread associated with a JSF request lifecycle. A FacesContext object is created for every request and disposed at the end of the request. The reason you can fetch it via a static call is because it is set to a ThreadLocal at the start of the request. The lifecycle of a FacesContext bears no relation to that of a ServletContext.

    Maybe this isn’t enough (it sounds like you’ve already been down this route), but you should be able to use a ServletContextListener to do what you want. Just make sure that any calls to the FacesContext are kept in the JSP’s request thread.

    web.xml:

    <listener>     <listener-class>appobj.MyApplicationContextListener</listener-class> </listener> 

    Implementation:

    public class MyApplicationContextListener implements ServletContextListener {      private static final String FOO = 'foo';      public void contextInitialized(ServletContextEvent event) {         MyObject myObject = new MyObject();         event.getServletContext().setAttribute(FOO, myObject);     }      public void contextDestroyed(ServletContextEvent event) {         MyObject myObject = (MyObject) event.getServletContext().getAttribute(                 FOO);         try {             event.getServletContext().removeAttribute(FOO);         } finally {             myObject.dispose();         }     }  } 

    You can address this object via the JSF application scope (or just directly if no other variable exists with the same name):

    <f:view>     <h:outputText value='#{applicationScope.foo.value}' />     <h:outputText value='#{foo.value}' /> </f:view> 

    If you wish to retrieve the object in a JSF managed bean, you can get it from the ExternalContext:

    FacesContext.getCurrentInstance()             .getExternalContext().getApplicationMap().get('foo'); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sun does not release Java for OS X, Apple does… May 12, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer You need to enclose all the tabs in the same… May 12, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer Windows didn't use to call them "mount points" [edit: it… May 12, 2026 at 12:40 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.