1
votes

I have a JSF 2.2 application with login page I need to control users access to the pages so I wrote a web filter but the problem is that it always return null when I try to access the session scoped JSF managed bean. I already logged in and I made sure that the login works fine and the user set correctly but still when I access the bean it returns null

here is my filter code to access the bean

UserLogin loginBean = (UserLogin) ((HttpServletRequest) request).getSession().getAttribute("userLogin");

here is the bean code with the login in method

@Named(value = "userLogin") @SessionScoped @ManagedBean
public class UserLogin implements Serializable {

  /**
   * Creates a new instance of UserLogin
   */
  public UserLogin() {}
  private static final long serialVersionUID = 1520318172495977648L;
  private User user = null;

  String email, password;

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }



  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }



  public void login() {
    UserHelper userHelper = new UserHelper();
    user = userHelper.Login(email, password);
    if (user != null) {
      FacesMessage msg = new FacesMessage("Successful", "Welcomesss :" + user.getFirstname());
      FacesContext.getCurrentInstance().addMessage(null, msg);
    } else {
      FacesMessage msg = new FacesMessage("Failed", "Wrong Usernames or password.");
      msg.setSeverity(FacesMessage.SEVERITY_ERROR);
      FacesContext.getCurrentInstance().addMessage(null, msg);
    }

  }

}

I wrote a sample code to retrive all the session attrbuties but I couldn't find userLogin here is the method along with its output

 Enumeration attrs = ((HttpServletRequest) request).getSession().getAttributeNames();
 while (attrs.hasMoreElements()) {

   LOG.log(Logger.Level.FATAL, attrs.nextElement());
 }






FATAL: WELD_S #10
FATAL:   com.sun.faces.application.view.activeViewMaps
FATAL:   org.jboss.weld.context.conversation.ConversationIdGenerator
FATAL:   com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap
FATAL:   org.jboss.weld.context.ConversationContext.conversations
FATAL:   com.sun.faces.application.view.activeViewContexts
FATAL:   javax.faces.request.charset
FATAL:   org.jboss.weld.context.beanstore.http.LockStore
1
What is the import of your @SessionScoped ?Tarik
I want to get the values of this bean later from the session in the filter.Jim
@Ahemd Saad I understand, but I asked about the import statement of SessionScoped, "import ... " ?Tarik
import javax.enterprise.context.SessionScoped;Jim

1 Answers

2
votes

You are using both @ManagedBean and @Named annotations, you must use only one of them.

As you are using javax.enterprise.context.SessionScoped for the @SessionScoped which mean that you are using CDI Managed beans, then you should only use @Named to make your bean CDI managed.

You can check BalusC's answer for more informations about the difference between @Named and @ManagedBean annotations

Update:

Regarding your comment, you don't need to use the:

UserLogin loginBean = (UserLogin) ((HttpServletRequest) request).getSession().getAttribute("userLogin")

You can just use it like this:

public class YourFilter { 

@Inject
UserLogin loginBean;

...

}