13 Replies Latest reply on Jan 15, 2008 1:50 AM by Nicklas Karlsson

    How can I get the session id?

    Chico Chen Newbie

      I use below code, but FacesContext.getCurrentInstance() always return null.

      FacesContext facesContext = FacesContext.getCurrentInstance();
       ExternalContext externalContext = facesContext.getExternalContext();
       HttpSession session = (HttpSession)externalContext.getSession(true);
       String sessionId = session.getId();



      package com.eastidea.menglishweb.action;
      
      import javax.faces.context.ExternalContext;
      import javax.faces.context.FacesContext;
      import javax.persistence.EntityManager;
      import javax.persistence.NoResultException;
      import javax.servlet.http.HttpSession;
      
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.security.Identity;
      
      import com.eastidea.menglishweb.action.base.BaseActionImpl;
      import com.eastidea.menglishweb.entity.User;
      
      @Name("authenticateAction")
      public class AuthenticateActionImpl extends BaseActionImpl implements
       AuthenticateAction {
      
       private static final long serialVersionUID = 7601102159656515184L;
      
       @In
       private EntityManager entityManager;
      
       @In
       private Identity identity;
      
       public boolean authenticate() {
       try {
       User user = (User) entityManager
       .createQuery(
       "from User where username = :username and password = :password")
       .setParameter("username", identity.getUsername())
       .setParameter("password", identity.getPassword())
       .getSingleResult();
       setCurrentUser(user);
      
       FacesContext facesContext = FacesContext.getCurrentInstance();
       ExternalContext externalContext = facesContext.getExternalContext();
       HttpSession session = (HttpSession)externalContext.getSession(true);
       String sessionId = session.getId();
      
       return true;
       } catch (NoResultException ex) {
       return false;
       }
       }
      }