0

I have a managed bean which is in the session scope

@ManagedBean(name = "loginAction")
@SessionScoped
public class LoginAction implements OnReceiveListner, Serializable {}

In a servlet call i am getting this using

LoginAction loginAction = (LoginAction) request.getSession().getAttribute("loginAction");

Now I was wondering if i can call this using CDI for each session. Like

@Inject LoginAction loginAction(current session)

How can i do this

LynAs
  • 6,407
  • 14
  • 48
  • 83
  • The `LoginAction` bean is decorated by `@ManagedBean` (I assumed it is from the package `javax.faces.bean`. Thus not from `javax.annotation`). Thus it is a JSF managed bean. Why not just have CDI manage your beans? If it is a CDI bean, it can be injected into any other bean(s) with any scope - broader or narrower. What are you actually trying to do? – Tiny Dec 26 '14 at 05:37
  • Could you please give me some example code. I am trying to call a function of `LoginAction` it is in the Session scope so every user should see his session `LoginAction` object value. – LynAs Dec 26 '14 at 06:26
  • You can simply inject that bean (provided that it is managed by a CDI manager/implementation like Weld) into the target Servlet using the `@Inject` annotation. JSF managed beans are managed by the JSF framework. Thus they are not available in a Servlet. JSF stores session beans in a `java.util.Map` using the bean name as a key. Thus it is available as a session attribute. You are apparently doing that right way. Just that the bean itself cannot be injected into the target Servlet unless it is managed by CDI. – Tiny Dec 26 '14 at 07:03
  • http://stackoverflow.com/q/2633112/1391249, http://stackoverflow.com/q/14461989/1391249, http://stackoverflow.com/q/3920069/1391249 – Tiny Dec 26 '14 at 07:08
  • ok got it post it as answer i will accept it. thanks – LynAs Dec 26 '14 at 07:51

0 Answers0