Get JSF managed bean by name in any Servlet related class

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:



@ManagedBean(name="myBean") 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.

Example of how to parse exiftool JSON output in Haskell


MooTools: Events
In a Servlet, you can get request scoped beans by:.
Sort special Json (multilevel)
Bean bean = (Bean) request.getAttribute("beanName"); 
and session scoped beans by:.
Saving a Json extraction in Lift
Bean bean = (Bean) request.getSession().getAttribute("beanName"); 
and application scoped beans by:.
what's basic difference between JsonStore and JsonReader in context to
Bean bean = (Bean) getServletContext().getAttribute("beanName"); 
If you're running in a dependency injection capable framework/container and the bean is managed by CDI's @Named instead of JSF's @ManagedBean, it's even more easy:.
Could CouchDB benefit significantly from the use of BERT instead of JSON? [closed]
@Inject private Bean bean; 
Regardless of the scope, when you're actually inside the FacesContext (i.e.

Json Vs Conventional Technologies
the current HTTP request has been served through the FacesServlet), then the normal JSF2 way is using Application#evaluateExpressionGet():.
Simple structured data (INI, YAML, JSON, etc) editor application
FacesContext context = FacesContext.getCurrentInstance(); Bean bean = context.getApplication().evaluateExpressionGet(context, "#{beanName}", Bean.class); 
which can be convenienced as follows:.
@SuppressWarnings("unchecked") public static <T> T findBean(String beanName) {     FacesContext context = FacesContext.getCurrentInstance();     return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class); } 
and can be used as follows:.
Bean bean = findBean("bean"); 
However, when you're already inside a @ManagedBean, then using @ManagedProperty is cleaner since it's more declarative..
@ManagedProperty("#{bean}") private Bean bean; 


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..


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); 


You can get the managed bean by passing the name:.
public static Object getBean(String beanName){     Object bean = null;     FacesContext fc = FacesContext.getCurrentInstance();     if(fc!=null){          ELContext elContext = fc.getELContext();          bean = elContext.getELResolver().getValue(elContext, null, beanName);     }      return bean; } 


I use this:.
public static <T> T getBean(Class<T> clazz) {     try {         String beanName = getBeanName(clazz);         FacesContext facesContext = FacesContext.getCurrentInstance();         return facesContext.getApplication().evaluateExpressionGet(facesContext, "#{" + beanName + "}", clazz);     //return facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, nomeBean);     } catch (Exception ex) {         return null;     } }  public static <T> String getBeanName(Class<T> clazz) {     ManagedBean managedBean = clazz.getAnnotation(ManagedBean.class);     String beanName =;      if (StringHelper.isNullOrEmpty(beanName)) {         beanName = clazz.getSimpleName();         beanName = Character.toLowerCase(beanName.charAt(0)) + beanName.substring(1);     }      return beanName; } 
And then call:.
MyManageBean bean = getBean(MyManageBean.class); 
This way you can refactor your code and track usages without problems..


I had same requirement.

. I have used the below way to get it.

. I had session scoped bean.

@ManagedBean(name="mb") @SessionScopedpublic  class ManagedBean {      -------- } 
I have used the below code in my servlet doPost() method.

ManagedBean mb = (ManagedBean) request.getSession().getAttribute("mb"); 
it solved my problem..

74 out of 100 based on 49 user ratings 1024 reviews