Get JSF managed bean by name in any Servlet related class

I'm trying to write a custom servlet (for AJAX/JSON) in which I would like to reference my @ManagedBeans by name. I'm hoping to map:



public class MyBean {
    public String getMyProperty();

Is it possible to load a bean by name from a regular servlet? Is there a JSF servlet or helper I could use for it?

I seem to be spoilt by Spring in which all this is too obvious.

How to access managed bean and session bean from Servlet , JSF stores session scoped managed beans as session attribute using managed bean name as key. So the following should work (assuming that JSF has� In the above code we have declared our Bean as ManagedBean by using the annotation @ManagedBean.. We can set the attribute name to define how we will reference the bean in our Jsp or JSF pages.The attribute eager = true, means bean is created before it is requested for the first time otherwise lazy initialization is used in which bean will be created only when it is requested.

I use the following method:

public static <T> T getBean(final String beanName, final Class<T> clazz) {
    ELContext elContext = FacesContext.getCurrentInstance().getELContext();
    return (T) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, beanName);

This allows me to get the returned object in a typed manner.

Accessing one Managed Bean from another in JSF, There are two ways for one managed bean to access another managed bean information. 1 . Using Dependency Injection in JSF2.0. We can use� Get JSF managed bean by name in any Servlet related class Unrelated to the concrete problem, this may not be the best way. Look at @ManagedProperty if possible.

Have you tried an approach like on this link? I'm not sure if createValueBinding() is still available but code like this should be accessible from a plain old Servlet. This does require to bean to already exist.

 FacesContext context = FacesContext.getCurrentInstance();  
 Application app = context.getApplication();
 // May be deprecated
 ValueBinding binding = app.createValueBinding("#{" + expr + "}"); 
 Object value = binding.getValue(context);

java ee Get JSF managed bean by name in any Servlet related class , java ee Get JSF managed bean by name in any Servlet related class? @Inject private Bean bean; @ManagedProperty("#{bean}") private Bean bean; Browse other questions tagged jsf-2 servlet-filters managed-bean session-scope or ask your own question. Blog We’re Rewarding the Question Askers

You can get the managed bean by passing the name:

public static Object getBean(String beanName){
    Object bean = null;
    FacesContext fc = FacesContext.getCurrentInstance();
         ELContext elContext = fc.getELContext();
         bean = elContext.getELResolver().getValue(elContext, null, beanName);

    return bean;

Retrieve ViewScoped bean from session map – Michael's blog, The HttpSession object contains a map of all session objects. /2633112/get-jsf- managed-bean-by-name-in-any-servlet-related-class. Configuring Lists and Maps as managed beans in JSF. managed-beans.xml. We can configure Lists and Maps as Managed Beans using JSF. We assign a name that we can use in the view to reference the bean. We must provide an implementation class of the Map or List because JSF cannot choose an implementation at runtime.

I had same requirement.

I have used the below way to get it.

I had session scoped bean.

class ManagedBean {

I have used the below code in my servlet doPost() method.

ManagedBean mb = (ManagedBean) request.getSession().getAttribute("mb");

it solved my problem.

Configuring Managed Beans, Customize a bean's properties without any additional code In the New File wizard, select the JavaServer Faces category, then select JSF Faces The managed-bean-class element defines the fully qualified name of the JavaBeans To access the area code at the time the page is rendered, refer to the property from the� package net.javabeat.jsf; public class JSFTutorialBean { private String message = &quot;This is a JSF Tutorial Bean&quot;; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }

In the previous releases of jsf and before jsf 2 coming into existence, the managed beans were defined by providing the <managed-bean/> xml fragment into the jsf configuration file (faces-config.xml), but since jsf 2 the managed beans are defined using the old one and using a new fashion way, through the using of annotations.

@ManagedBean marks a bean to be a managed bean with the name specified in name attribute. If the name attribute is not specified, then the managed bean name will default to class name portion of the fully qualified class name. In our case, it would be helloWorld. Another important attribute is eager.

This is an older approach to configure bean into xml file. In this approach, we have to create a xml file named faces-config.xml. JSF provides <managec-bean> tag to configure the bean. In the above example, we are listing bean-name, bean-class and bean-scope. So, it can be accessible in the project. Configuring Managed Bean using Annotations

  • I'm not sure if you can use these new annotations outside JSF/EL, but I'd start by looking at the JSR 299 spec:
  • Other people having problems with similar issues can also check (related to AJAX and request mapping/handling, not getting beans by name)
  • You're second suggestion about just injecting the bean was so amazingly simple I had totally overlooked it. As always, your response is perfectly to the point. Thanks so much for your work here on SO.
  • In the meantime (speaking as of JSF 2.2) it seems like the method evaluateExpressionGet was extended with a third parameter that allows to specify the expected class so casting won't be necessary anymore. PostBean bean = context.getApplication().evaluateExpressionGet(context, "#{beanName}", PostBean.class);
  • @Marc: Has been in from the beginning. Was just a leftover from a copypaste mistake I guess. Answer has been corrected. Thank you for notifying.
  • FacesContext is available even though the static utility method findBean() is defined inside a plain Java class. How is it available there in a plain Java class which is not managed by JSF?
  • @Tiny: it's in turn called by a JSF artifact within the same thread.
  • This is already covered by the currently accepted answer and even in a more convenient way (Class argument is namely unnecessary in this construct).
  • This probably won't work in a regular servlet. The FacesContext is a per-request thread-local artefact set up by the JSF lifecycle (usually the FacesServlet).
  • ValueBinding is deprecated since JSF 1.2 over 4 years ago.
  • @BalusC: It shows how up to date I am lol. On a sidenote, using a search engine to research techniques is turning out to be counterproductive with all the old information out there. @McDowell: That actually makes sense. I'll do a test just to see what happens.
  • I try to to this from a servlet but it doesn`t work.
  • What kind of servlet do you use? mate
  • It is HttpServlet.