Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8077849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:36:15+00:00 2026-06-05T15:36:15+00:00

After autheticating my user, I want to put a reference in the session to

  • 0

After autheticating my user, I want to put a reference in the session to current logged in user.

Here how I do it in the setCurrentUser method :

FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
session.setAttribute("CURRENT_USER", currentUser);

Unfortunately, the session reference is always null !

Alternatively, I tried with the sessionMap

FacesContext facesContext = FacesContext.getCurrentInstance();
Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
sessionMap.put("CURRENT_USER", currentUser);

It miserably failed with this exception :

java.lang.UnsupportedOperationException
    at java.util.AbstractMap.put(AbstractMap.java:186)
    (...)

What am I doing wrong ?

The full code of my controller

UserController.java

public class UserController implements Filter {
    private FilterConfig fc;
    private static final String CURRENT_USER = "CURRENT_USER";

    public void init(FilterConfig filterConfig) throws ServletException {
        fc = filterConfig;
        log(">> Filter initialized");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // Authenticate user
        // ...

        // Save refernce in Session
        setCurrentUser(currentUser);

        //(...)
    }

    public static void setCurrentUser(User u) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
        session.setAttribute(CURRENT_USER, u);// session is always NULL
    }

    public static User getCurrentUser() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);       
        return (User)session.getAttribute(CURRENT_USER);
    }

    //...   
}

JSF 2.0
JBoss 5.1.0.GA

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:36:17+00:00Added an answer on June 5, 2026 at 3:36 pm

    The FacesContext is not available in a Filter as the Filter is invoked before the FacesServlet.

    You should be getting the session from the request argument instead.

    HttpSession session = ((HttpServletRequest) request).getSession();
    session.setAttribute("user", currentUser);
    // ...
    

    Once you’re in JSF context (e.g. inside a JSF managed bean or a JSF view), then this will be available by getSessionMap() on the very same attribute name

    User user = (User) externalContext.getSessionMap().get("user");
    

    Or just by #{user} in EL:

    @ManagedProperty("#{user}")
    private User user;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to load in a user's home timeline after authenticating through 'sign it
After creating roles and granting privileges to them, I want to grant the privileges
After constructing the bean, I want to retrieve data from the database, using the
After authenticating a user using CXF WSAuthentication Interceptor... I need the username in the
I have a viewcontroller containing a TabController. Before this loads, I want a user
After authenticating a user and posting to their wall, it gives the user a
What happens after authenticating using openid? In a simple case, the user-agent is redirected
After doing a chunk of reading about authenticating users (both here in SO land
My application is authenticating using RSA authentication manager Once the user is logged in,
I'm using this line after authenticating with Omniauth: sign_in_and_redirect @user, :event => :authentication But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.