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

  • Home
  • SEARCH
  • 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 1030917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:50:49+00:00 2026-05-16T13:50:49+00:00

I need to know what’s the best method for accessing a JSF managedBean (which

  • 0

I need to know what’s the best method for accessing a JSF managedBean (which is defined having application scope) from a servlet.
Currently I have something like this in my servlet:

  MyApplicationScopeBean bean = null;
  try {
   FacesContext fContext = FacesUtil.getFacesContext(req, resp);
   ServletContext sc = (ServletContext) fContext.getExternalContext().getContext();
   bean = (MyApplicationScopeBean) sc.getAttribute("myManagedBean");   
  } catch (Exception e) {
   e.printStackTrace();
  }

FacesUtil.java (as described in http://balusc.blogspot.com/2006/06/communication-in-jsf.html):

import javax.faces.FactoryFinder;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FacesUtil {
    // Getters -----------------------------------------------------------------------------------
    public static FacesContext getFacesContext(
        HttpServletRequest request, HttpServletResponse response)
    {
        // Get current FacesContext.
        FacesContext facesContext = FacesContext.getCurrentInstance();
        // Check current FacesContext.
        if (facesContext == null) {
            // Create new Lifecycle.
            LifecycleFactory lifecycleFactory = (LifecycleFactory)
                FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 
            Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
            // Create new FacesContext.
            FacesContextFactory contextFactory  = (FacesContextFactory)
                FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
            facesContext = contextFactory.getFacesContext(
                request.getSession().getServletContext(), request, response, lifecycle);
            // Create new View.
            UIViewRoot view = facesContext.getApplication().getViewHandler().createView(
                facesContext, "");
            facesContext.setViewRoot(view);                
            // Set current FacesContext.
            FacesContextWrapper.setCurrentInstance(facesContext);
        }
        return facesContext;
    }
    // Helpers -----------------------------------------------------------------------------------
    // Wrap the protected FacesContext.setCurrentInstance() in a inner class.
    private static abstract class FacesContextWrapper extends FacesContext {
        protected static void setCurrentInstance(FacesContext facesContext) {
            FacesContext.setCurrentInstance(facesContext);
        }
    }     
}

I always get a null when trying to access the bean from the servlet.
What are your suggestions?
I’m running JSF 1.2 on Tomcat 6

Thanks for your help.

  • 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-16T13:50:50+00:00Added an answer on May 16, 2026 at 1:50 pm

    JSF stores application scoped managed beans just in the ServletContext. In servlets, the ServletContext is just available by the inherited getServletContext() method. You don’t need to manually create a whole FacesContext around it. That’s only an unnecessarily expensive task for this purpose.

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Bean bean = (Bean) getServletContext().getAttribute("bean");
        // ...
    }
    

    If it returns null, then it simply means that JSF hasn’t kicked in yet to auto-create the bean for you (i.e. the servlet is called too early). You would then need to create and store it yourself. It will be used by JSF if the managed bean name (the attribute key) is the same.

        if (bean == null) {
            bean = new Bean();
            getServletContext().setAttribute("bean", bean);
        }
    

    That said, what’s the purpose of this servlet? Aren’t you trying to achieve some functional requirement the wrong way?

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

Sidebar

Related Questions

I need to know which gtktreeview was passed to a function from the handler
Need to know which event triggered after text selection done in Apple iPad. It
I need to know how to exclude files/folders from php generated zip. Here is
I need to know which iOS device is currently running app (saying more exactly
I need to know which version control systems support single checkout? When I say
I need to know what is the best way to consume an XML SOAP
I need to know which users are online on the website and for this
I need to know that how to read the directory path from a .properties
I need to know how to extract raw voice data from android real time.
Hallo. I need know user name of user which run browser. If I use

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.