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 6095663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:50:14+00:00 2026-05-23T12:50:14+00:00

I get the username of the connected user (using j_security_check) this way, through a

  • 0

I get the username of the connected user (using j_security_check) this way, through a managed bean:

......
    username =   FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();

And then display it in a jsf page this way : #{userBean.username}
But I figured no way to get the number of connected users and get their role.
In other words, I want to display besides the username, the user role and the number of connected users.
How can I achieve this!?
Thanks in advance for your help!

EDIT:
I can now get the Role of the connected user, using a namedquery in a managed bean :

public Users getUserRole(){
      try {
            Users auser = (Users)
            em.createNamedQuery("Users.findByUsername").
                    setParameter("username", getRemoteUser()).getSingleResult();
            return auser; 
        } catch (NoResultException nre) {
            JsfUtil.addErrorMessage(nre, "getUserRole Error");
            return null;
        }

    }

and in the xhtml page:

<h:outputLabel for="rolefacet" value="Role: "/>
  <h:outputFormat id="rolefacet" value="#{UserBean.userRole.ugroup}" /> 

while ugroup is the role name in the Users entity class.


EDIT: One solution that still does not work for me is to add a HttpSessionListener to my web.xml:

package beans;

/**
 *
 * @author med81
 */

import java.io.Serializable;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.ArrayList;

import javax.faces.context.FacesContext;


public class SessionCounter implements Serializable, HttpSessionListener {

    private List sessions = new ArrayList();
   Object  s =  FacesContext.getCurrentInstance().getExternalContext().getSession(false);

    public Object getS() {
        return s;
    }

    public void setS(Object s) {
        this.s = s;
    }


    public SessionCounter() {
    }


    public void sessionCreated(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        sessions.add(session.getId());

        session.setAttribute("counter", this);
    }


    public void sessionDestroyed(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        sessions.remove(session.getId());

        session.setAttribute("counter", this);
    }

    /**
     * 
     * @return size of the session list
     */
    public int getActiveSessionNumber() {
        return sessions.size();
    }


}
  • 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-05-23T12:50:15+00:00Added an answer on May 23, 2026 at 12:50 pm

    Here’s a basic kickoff example how you could do it when you’re on Servlet 3.0 and thus are able to utilize programmatic login by the new HttpServletRequest#login() API.

    The login form: login.xhtml

    <h:form>
        <h:inputText value="#{user.username}" />
        <h:inputSecret value="#{user.password}" />
        <h:commandButton value="Login" action="#{user.login}" />
        <h:messages />
    </h:form>
    

    The user manager bean: com.example.UserManager

    @ManagedBean(name="user")
    @SessionScoped
    public class UserManager implements Serializable {
    
        private String username;
        private String password;
        private User current;
    
        @EJB
        private UserService userService;
    
        @ManagedProperty("#{loginManager.logins}")
        private Set<User> logins;
    
        public String login() {
            FacesContext context = FacesContext.getCurrentInstance();
            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
    
            try {
                request.login(username, password);
                current = userService.find(username, password);
            } catch (ServletException e) {
                // Unknown login. Will be handled later in current==null check.
            }
    
            if (current == null) {
                context.addMessage(null, new FacesMessage("Unknown login"));
                return null;
            } else {
                logins.add(current)
                return "home?faces-redirect=true";
            }
        }
    
        public String logout() {
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
            return "login?faces-redirect=true";
        }
    
        // ...
    }
    

    The logout (and session invalidate) listener: com.example.LogoutListener

    @WebListener
    public class LogoutListener implements HttpSessionListener {
    
        @Override
        public void sessionCreated(HttpSessionEvent event) {
            // NOOP.
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent event) {
            UserManager userManager = (UserManager) event.getSession().getAttribute("user");
            if (userManager != null && userManager.getCurrent() != null) {
                userManager.getLogins().remove(userManager.getCurrent());
            }
        }
    
    }
    

    (Do not do this in logout() method! It’s the session invalidation which triggers this, the session invalidation will take place when logout() is called OR when session has expired)

    In any logged-in view you can obtain the current user and the login count as follows:

    <p>Welcome, #{user.current.name}!</p>
    <p>Total logged in users: #{user.logins.size()}</p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using c# membership provider and I get the username from a query
I have an action that relies on User.Identity.Name to get the username of the
I'm using the following to get the Facebook username: [facebook requestWithGraphPath:@me andDelegate:facebook]; from my
How to get user roles from Membership provider? Suppose I have a username and
I'm trying to look up user's username using $_GET but not actually seing the
First, let's get the security considerations out of the way. I'm using simple authentication
I have two tables User and UserRole which are they connected using a link
I'm trying to get the user's login details from the database using $SETTINGS[admin_username] and
On iPhone, how do I show a login screen to get username and password
How can I get the username from the webservice my Webservice is configured under

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.