0

I have a JSF session scope bean and I'm keeping current user(logged in) information in that bean. I also have a class which is neither servlet nor bean, its just a class. I want to access the jsf bean and get current user information in the class. I've found a solution for servlet to access jsf bean but i couldnt find a solution for this problem. Is there any way to do that?

Thank!

Ahmet DAL
  • 4,445
  • 9
  • 47
  • 71

1 Answers1

1

If the instance of the mentioned class is running in the same thread as the HTTP request thread which has invoked the FacesServlet, then you can just get it by the FacesContext and then Application#evaluateExpressionGet(). See also Get JSF managed bean by name in any Servlet related class

If the instance of the mentioned class is running in a different thread, then you'd need to pass the desired information beforehand to the class' constructor, method or to store the desired information in some shared datasource which both the JSF webapp and the standalone class have access to, such as a database, a local disk file system file or the JNDI context. Depending on the context and the environment, CDI's @Named+@Inject may also be the solution.

The "best way" depends on the concrete functional requirement which is not clear from the question, so I can't point out the right way nor give any kickoff examples.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555