22

I need to persist just one object in session scope of my JSF application. Where do I define a session variable, and how do I get and set it from either a view file or backing bean?

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

3 Answers3

35

Two general ways:

  • Store it in 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 a @SessionScoped bean which you inject in your @RequestScoped bean.

    sessionBean.setSomeVariable(yourVariable);
    

    And then later:

    SomeObject yourVariable = sessionBean.getSomeVariable();
    

    You can get a @Named @SessionScoped into a @Named @RequestScoped via @Inject.

    @Inject
    private SessionBean sessionBean;
    

    Or, if you're not using CDI yet, you can get a @ManagedBean @SessionScoped into a @ManagedBean @RequestScoped via @ManagedProperty.

    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean; // +getter+setter
    
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • What if externalContext is null? Does something need to be configured so the client is using a session(like telling it to store a cookie)? – Disgruntled Java Developer Dec 21 '10 at 23:47
  • 1
    It's never null. And no, the session is basically already backed by a cookie. In the very rare case when the client has cookies disabled (which you **cannot** enable from the server side on), then JSF will automatically take care about URL rewriting with `jsessionid` (only when you use ``, `` and so on instead of plain HTML ``, `
    `, etc).
    – BalusC Dec 21 '10 at 23:48
2

Just moving along to JSF 2.2 and CDI 1.2 - Injection will at least be simpler. Keeping in line with the original answer of @BalusC:

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

with

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 notes to be made - particularly the switch to @Named, and the Fully Qualified Package Name for RequestScoped and SessionScoped. Also for making a Class SessionScoped it should also be made Serializable.

The addition of @Inject makes it really simple - but understand that the injected sessionBean object is only available after construction, not during. This means you do not have access to sessionBean within the constructor of RequestBean. This is solved by the use of @PostConstruct which gets triggered after injection is complete, and RequestBean is otherwise fully initialized.

YoYo
  • 9,157
  • 8
  • 57
  • 74
0

When you call the method FacesContext.getCurrentInstance() it will return the current thread but in case of P.S.V.M there will be no thread running in application context. So you get a NPE.

Better 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 into a session bean. You can use the code to validate users.

CSchulz
  • 10,882
  • 11
  • 60
  • 114
ifti
  • 649
  • 1
  • 11
  • 25