0

I have a Bean annotated as @SessionScoped:

@ManagedBean(name="bUser")
@SessionScoped
public class BUser implements Serializable {
    private Integer userId = 0;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }
}

And I have a filter class (javax.servlet.Filter), which will be used as a router:

public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        // Get bUser bean property

        chain.doFilter(request, response);
    }

    // [...]
}

How can I access the bUser bean and its properties?

Df.fpm
  • 171
  • 1
  • 4
  • 13
  • 1
    See [Get JSF managed bean by name in any Servlet related class](https://stackoverflow.com/questions/2633112) – f_puras May 20 '16 at 09:20
  • I read it before. But in my case: `BUser bUser = (BUser) request.getAttribute("bUser");` returns null. `request.getSession()` does not exists. – Df.fpm May 20 '16 at 09:25
  • 1
    Of course session attributes are not available on the request. In order to get the session, [just cast request to `HttpServletRequest`](http://stackoverflow.com/questions/15009784/session-variables-in-servletrequest). – BalusC May 20 '16 at 09:29
  • By the way, since you're as per your edit apparently using JSF 2.3 while this is not final yet, I'd like to warn that **JSF 2.3 will deprecate `@ManagedBean`**. See also http://stackoverflow.com/a/4347707 Additional advantage with CDI is that you don't need to manually mess with loose attribtues this way. – BalusC May 20 '16 at 09:31
  • @Df.fpm This will be the case on the user's very first call to your app. The session object is about to be created, your `BUser` bean will be created on its first use within a JSF page. – f_puras May 20 '16 at 09:33
  • BalusC, f_puras, you are right. Should i remove post? – Df.fpm May 20 '16 at 09:36
  • Not necessary. Duplicates feed search engines with new keywords. Moreover, deletions may account to question bans. – BalusC May 20 '16 at 09:40
  • @Df.fpm Just leave it here. The original question did not explicitely mention Filters, so you're adding a new aspect. – f_puras May 20 '16 at 09:49
  • Who voted for reopen? Please undo that vote. That makes no sense. – BalusC May 20 '16 at 10:10

0 Answers0