0

In my attempts to find out the reason for the problems stated in this question: JSF2 slow page loading, I would like to time a user session filter I use. I've created a timer that is an ApplicationScoped bean.

My question is if it's possible to access this bean as a managed property from the filter. In my other beans I can do this, although in the filter the managed property is always null.

Community
  • 1
  • 1
nivis
  • 913
  • 3
  • 17
  • 34
  • When you said "filter", do you actually mean `javax.servlet.Filter`? (if so, you should have used `[servlet-filters]` tag, not `[filter]`!) The currently accepted answer makes in this case then no sense. – BalusC Nov 28 '12 at 13:01
  • You're right. I've changed the tag to the correct one. Regarding the answer, the last suggestion worked. I'm still not sure how the mechanism of ManagedProperty works in this context though. – nivis Nov 28 '12 at 14:05
  • 2
    The `FacesContext` is normally not available in a servlet filter at all, so why it works for you is beyond me. The managed property works in a managed bean only, however a servlet filter is not a managed bean. Your question is then basically answered by the very same answer as on this question: http://stackoverflow.com/questions/2633112/jsf-get-managed-bean-by-name – BalusC Nov 28 '12 at 14:06
  • Thanks for enlightening me and the examples in the link. – nivis Nov 28 '12 at 14:41

1 Answers1

3

Updated Answer:

Access your application scoped bean from filter in the follwoing way

ServletContext context = req.getServletContext();
MyAppBean myAppBean = (MyAppBean) context.getAttribute("myAppBean");

Access your session scoped bean from filter in the following way

HttpSession session = ((HttpServletRequest) req).getSession(false);
MyAppBean myAppBean = (MyAppBean ) session.getAttribute("myAppBean");

Original Answer :

Yes you can , make it (eager = true) , and add the needed annotations

@ManagedBean(eager = true)
@ApplicationScoped
public class MyAppBean { }

and in your sesion scoped bean access it like this

@ManagedProperty(value = "#{myAppBean }")
private MyAppBean myAppBean; //add getter and setter

If your bean is not a @ManagedBean you can access it from the ApplicationMap like this

MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
    getExternalContext().getApplicationMap().get("country");

If the bean you want to access is a @SessionScoped you can grab it from the SessionMap like this

MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
    getExternalContext().getSessionMap().get("country");

Here you got some nice tutorial on how to access beans from a non managed beans

Access A Managed Bean From Event Listener – JSF

Daniel
  • 36,833
  • 10
  • 119
  • 200
  • It made no difference, I'm afraid. Does the filter need to be a bean for this to work? Currently it's not. – nivis Nov 28 '12 at 10:10
  • It worked using your last solution. Registering the filter as a ManagedBean did not though. Weird. Thanks for your help! – nivis Nov 28 '12 at 11:18