How to get and set an object in a session area in JSF?

I need to save only one object in the session area of ​​my JSF application. Where can I define a session variable and how to get and set it from a bean view or support file?

+19
session jsf
Dec 21 2018-10-12T00:
source share
3 answers

Two main ways:

  • Save it to ExternalContext#getSessionMap()

     ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); Map<String, Object> sessionMap = externalContext.getSessionMap(); sessionMap.put("somekey", yourVariable); 

    And then later:

     SomeObject yourVariable = (SomeObject) sessionMap.get("somekey"); 
  • Or make it a property of the @SessionScoped bean, which you add to your @RequestScoped .

     sessionBean.setSomeVariable(yourVariable); 

    And then later:

     SomeObject yourVariable = sessionBean.getSomeVariable(); 

    You can get @Named @SessionScoped at @Named @RequestScoped via @Inject .

     @Inject private SessionBean sessionBean; 

    Or, if you are not already using CDI, you can get @ManagedBean @SessionScoped at @ManagedBean @RequestScoped via @ManagedProperty .

     @ManagedProperty("#{sessionBean}") private SessionBean sessionBean; // +getter+setter 
+30
Dec 21 2018-10-12T00:
source share

Just move on to JSF 2.2 and CDI 1.2. Injection will be at least easier. Saving according to @BalusC original answer:

 import javax.enterprise.context.RequestScoped; import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.inject.Named; @Named @RequestScoped public class RequestBean { @Inject private SessionBean sessionBean; // ... @PostConstruct void init() { // Interact with sessionBean during construction // but after Injection. } } 

from

 import java.io.Serializable; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; @Named @SessionScoped public class SessionBean implements Serializable { private SomeObject someVariable; // ... } 

There are several important @Named , in particular, switching to @Named and the full package name for RequestScoped and SessionScoped . Also, to create the SessionScoped class SessionScoped it must also be made Serializable .

Adding @Inject makes it very simple - but sessionBean that the sessionBean object you sessionBean is only available after building, not at time. This means that you do not have access to sessionBean inside the RequestBean constructor. This is solved using @PostConstruct , which starts after the injection is completed, and the RequestBean is otherwise fully initialized.

+2
Apr 10 '17 at 2:27
source share

When calling the FacesContext.getCurrentInstance() method, it will return the current stream, but in the case of PSVM, there will be no stream in the application context. So you get NPE.

Better to use something like this:

 public String checker() { SessionFactory factory = HibernateUtil.getSessionFactory(); Session session = factory.getCurrentSession(); session.beginTransaction(); Query q = session.createQuery("from UserLogin where UserId='" + uid.getUserId() + "'and Pswd='" + uid.getPswd() + "'and RoleId='" + uid.getRoleId() + "'"); setUid((UserLogin) q.uniqueResult()); System.out.println("upto here every thing is workind properly"); if (uid != null) { FacesContext context = FacesContext.getCurrentInstance(); HttpServletRequestrequest = (HttpServletRequest) context .getExternalContext().getRequest(); HttpSession appsession = request.getSession(true); if (appsession.isNew() == false) { appsession.invalidate(); appsession = request.getSession(true); } context.getExternalContext().getSessionMap().put("userbean", uid); session.close(); return uid.getRoleId(); } else return "invalid"; } 

and put it in a bean session. You can use the code to verify users.

0
Oct 11 '11 at 11:07
source share



All Articles