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 6354047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:30:50+00:00 2026-05-24T22:30:50+00:00

I have created a class StartupListener implements ServletContextListener. Inside @Override public void contextInitialized(ServletContextEvent contextEvent)

  • 0

I have created a class StartupListener implements ServletContextListener. Inside

@Override
public void contextInitialized(ServletContextEvent contextEvent) {

I create a timer which will be executed every 30 seconds. Inside this timer I want to set a parameter from my @ApplicationScoped – Bean in order to display the information from the timer to my website.

Here is what I did so far:

StartupListener.java

package de.whatever;

import java.util.Timer;
import javax.naming.Context;

public class CargoServiceStartupListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent contextEvent) {

    ....        

        ModuleScannerTask task = new ModuleScannerTask(contextEvent.getServletContext());
        Timer timer = new Timer("Scanner");
        timer.schedule(task, 0, 10000);

ModuleScannerTask.java

package de.whatever;

import java.util.Enumeration;

public class ModuleScannerTask extends TimerTask{

private ServletContext context;

public ModuleScannerTask (ServletContext context) {
    this.context = context;
}

@Override
public void run() {
      System.out.println("Start scanning ...");
          ...

ModuleBean.java

package de.whatever;

@ManagedBean (name="modOverview")
@ApplicationScoped
public class ModuleOverviewBean {

private String randomData;

My question is how to set data into randomData field in my application bean.

I read that I could load the attribute “modOverview” from the ServletContext, but that returns null.

What would be the correct way to load and set the values?

I am using JBoss (without spring).

Thanks
Hauke

  • 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. Editorial Team
    Editorial Team
    2026-05-24T22:30:50+00:00Added an answer on May 24, 2026 at 10:30 pm

    If it returns null then it simply means that JSF hasn’t created it yet. Just create one yourself. JSF won’t override it when already present in application scope. You just have to be aware that any managed properties of this class won’t be set automatically.

    ModuleOverviewBean bean = (ModuleOverviewBean) servletContext.getAttribute("modOverview");
    
    if (bean == null) {
        bean = new ModuleOverviewBean();
        servletContext.setAttribute("modOverview", bean);
    }
    
    // ...
    

    Unrelated to the problem, the java.util.Timer is a very bad way of doing the background job in a Java EE application. Look at least at ScheduledExecutorService, or since you’re already using JBoss, a @Singleton EJB with a @Schedule method.

    See also:

    • Refresh/Reload Application scope managed bean
    • Background timer task in JSP/Servlet web application

    Update: if setting of managed properties is also your concern, you could also try to annotate the managed bean with eager=true as follows so that JSF will autocreate it on webapp’s startup:

    @ManagedBean(name="modOverview", eager=true)
    

    I’m only not entirely sure if this will run before your ServletContextListener runs. I think it will. However, it will definitely work if you’re using a @Singleton EJB with @Schedule.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a class student with three properties like this public class Student
I have created a class Person that looks like this: public class Person {
I have created many class as my data class i.e: [DataContract] public class pCity
I have created a class which holds three classes as properties: public class Feeds
I'm building simple phonebook. Thus a have created a class Person: public class Person
I have created following class: public static class Current { public static class User
I'd like to create several instances of a class in CodeIgniter. I have created
I have created a class for a dashboard item which will hold information such
I have created UITableCellView class called NoteCell . The header defines the following: #import
I have created a class library in VB .NET. Some code in the library

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.