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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:28:27+00:00 2026-05-23T11:28:27+00:00

(Java EE 6 with Glassfish 3.1) I have a property file that I want

  • 0

(Java EE 6 with Glassfish 3.1)

I have a property file that I want to process only once at start up time, so I did this

public class Config implements ServletContextListener{

    private static final String CONFIG_FILE_PATH = "C:\\dev\\harry\\core.cfg";

    private static final String CONFIG_ATTRIBUTE_NAME = "config";

    private long startupTime;

    private ConfigRecord config;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        this.startupTime = System.currentTimeMillis() / 1000;
        this.config = new ConfigRecord(CONFIG_FILE_PATH); //Parse the property file
        sce.getServletContext().setAttribute(CONFIG_ATTRIBUTE_NAME, this);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //Nothing to do here
    }

    public ConfigRecord getConfig() {
        return config;
    }

    public long getStartupTime() {
        return startupTime;
    }
}

and in web.xml, i register it as follow

<listener>
    <listener-class>com.wf.docsys.core.servlet.Config</listener-class>
</listener>

Now how do I access the ConfigRecord config from the managed bean. I try this

@ManagedBean
@RequestScoped 
public class DisplayInbound {

    @EJB
    private CoreMainEJBLocal coreMainEJBLocal;

    @javax.ws.rs.core.Context
    private ServletContext servletContext;

    public void test(){
        Config config = (Config) servletContext.getAttribute("config")
        ConfigRecord configRecord = config.getConfig();
    }
}

I dont think it work. Got NullPointerException.

  • 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-23T11:28:28+00:00Added an answer on May 23, 2026 at 11:28 am

    That @Context annotation is only applicable in a JAX-RS controller, not in a JSF managed bean. You have to use @ManagedProperty instead. The ServletContext is available by ExternalContext#getContext(). The FacesContext itself is available by #{facesContext}.

    @ManagedProperty(value="#{facesContext.externalContext.context}")
    private ServletContext context;
    

    Or because you stored the listener as a servletcontext attribute, which is basically the same as the JSF application scope, you could also just set it as managed property by its attribute name:

    @ManagedProperty(value="#{config}")
    private Config config;
    

    But since you’re on JSF 2.0, I’d suggest to use an @ApplicationScoped @ManagedBean instead which is eagerly constructed. With @PostConstruct and @PreDestroy in such a bean you have similar hooks on webapp’s startup and shutdown as in a ServletContextListener.

    @ManagedBean(eager=true)
    @ApplicationScoped
    public void Config {
    
        @PostConstruct
        public void applicationInitialized() {
            // ...
        }
    
        @PreDestroy
        public void applicationDestroyed() {
             // ...
        }
    
    }
    

    You can inject it in another beans the usual @ManagedProperty way and access it in the views the usual EL way.

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

Sidebar

Related Questions

I have a Java-JSF Web Application on GlassFish, in which I want to use
I have a project that is Java EE 5 running on Glassfish. The IDE
Technology(Java EE 6 with Glassfish 3.1, Netbeans 7.0) I have an application client that
I have a WorkManager+ThreadPool under Glassfish that uses TIMED_WAIT, such as this: p: thread-pool-1;
i want to learn coding web apps with java using glassfish. i have bought
I have a Java project running on Glassfish that renders some ugly looking HTML.
We have a Java EE application running on Glassfish 3.1 that needs to accept
We have a Java EE-based web application running on a Glassfish app server cluster.
Although I'm primarily interested in Java/GlassFish with this question, I would certainly think it
So the basics are I have Glassfish 2.1 and Java 1.6.0_15 and it will

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.