1

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
Jim
  • 117
  • 1
  • 12

1 Answers1

2

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;

...

}
Community
  • 1
  • 1
Tarik
  • 4,546
  • 3
  • 33
  • 64
  • Hello Tarik, I have updated my bean to be @Named(value = "loginbean") @SessionScoped public class UserLogin implements Serializable {...} and now in my filter I am using the following line to get the value UserLogin loginBean = (UserLogin) httpServletRequest.getSession().getAttribute("loginbean"); – Jim Feb 27 '15 at 22:49
  • why you want to use that line to get the value? if you want to use it in another Bean you can just use @Inject – Tarik Feb 27 '15 at 22:52
  • I am using this line in the filter I am using filter to check if the user authenticated or no. – Jim Feb 27 '15 at 22:55
  • @AhmedSaad I added an example to my answer – Tarik Feb 27 '15 at 22:59
  • Man you are just awesome !! just please update your solution and remove the request scope.... now it works perfect ! Thx a ton – Jim Feb 27 '15 at 23:12