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

  • Home
  • SEARCH
  • 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 6037583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:05:43+00:00 2026-05-23T06:05:43+00:00

AFAIK, the httpsessionlisterner implementation listener class is get instantiated when the first session is

  • 0

AFAIK, the httpsessionlisterner implementation listener class is get instantiated when the first session is created.

Therefore, i would like access this instance because i need to count how many active session and display it some where and i would like to check which user is currently login. In the code below, there is list instance variable, i need to access this listener class in order to access the private variable.

@WebListener()
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {

  private List<HttpSession> sessionList;

  public SessionListener() {
    sessionList = new ArrayList<HttpSession>();
  }

  @Override
  public void sessionCreated(HttpSessionEvent se) {
    sessionList.add(se.getSession());
  }

  @Override
  public void sessionDestroyed(HttpSessionEvent se) {
    sessionList.remove(se.getSession());
  }

  @Override
  public void attributeAdded(HttpSessionBindingEvent event) {
  }

  @Override
  public void attributeRemoved(HttpSessionBindingEvent event) {

  }

  @Override
  public void attributeReplaced(HttpSessionBindingEvent event) {

  }

  /**
   * @return the sessionList
   */
  public List<HttpSession> getSessionList() {
    return Collections.unmodifiableList(sessionList);
  }

Please help.

Thanks.

  • 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-23T06:05:44+00:00Added an answer on May 23, 2026 at 6:05 am

    I have to make a few assumptions as you don’t say how your authentication method works.

    I will assume that your username will be contained in your HttpServletRequest (this is very common). Unless you have specifically coded your session to contain the username it will not contain the username of the authenticated user – the username is usually confined to the HttpServletRequest. Therefore you will not usually achieve your goal by using an HttpSessionListener. You probably know this but there are various “scopes”.

    • application scope (ServletContext) – per application
    • session scope (HttpSession) – per session
    • request scope (HttpServletRequest) – per request

    As I said, the username is usually stored in the request scope. You can access the session and application scopes from the request scope. You cannot access the request scope from the session scope (as this doesn’t make sense!).

    To solve your problem I would create a Map stored in the application scope and use a ServletFilter to populate it. You might want to use a time based cache (using the session time-out value) rather than a straight map as mostly sessions are started but timeout rather than get explicitly terminated by the user. kitty-cache is a really simple time based cache that you could use for this purpose.

    Anyway a code sketch (untested) might look something like this:

    public class AuthSessionCounter implements Filter {
    
        private static final String AUTHSESSIONS = "authsessions";
        private static ServletContext sc;
    
        public void init(FilterConfig filterConfig) throws ServletException {
            sc = filterConfig.getServletContext();
            HashMap<String, String> authsessions = new HashMap<String, String>();
            sc.setAttribute(AUTHSESSIONS, authsessions);
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest hsr = (HttpServletRequest) request;
            if (hsr.getRemoteUser() != null) {            
                HttpSession session = hsr.getSession();
                HashMap<String, String> authsessions = (HashMap<String, String>) sc.getAttribute(AUTHSESSIONS);
                if (!authsessions.containsKey(session.getId())) {
                    authsessions.put(session.getId(), hsr.getRemoteUser());
                    sc.setAttribute(AUTHSESSIONS, authsessions);
                }
            }
            chain.doFilter(request, response);
        }
    
        public void destroy(){}
    }
    

    You should now be able to obtain details of who and how many users are logged in from the authsessions Map that is stored in the application scope.

    I hope this helps,

    Mark

    UPDATE

    My authentication is works by checking
    the username and password in servlet
    and create a new session for it.

    In which case a HttpSessionListener might work for you – although as I mentioned before you probably still need to use a time based cache due to the way that most user sessions timeout rather than terminate. My untested code sketch would now look something like this:

    public class SessionCounter 
        implements HttpSessionListener, HttpSessionAttributeListener {
    
        private static final String AUTHSESSIONS = "authsessions";
        private static final String USERNAME = "username";
        private static ServletContext sc;
    
        public void sessionCreated(HttpSessionEvent se) {
            if (sc == null) {
                sc = se.getSession().getServletContext();
                HashMap<String, String> authsessions = new HashMap<String, String>();
                sc.setAttribute(AUTHSESSIONS, authsessions);
            }
        }
    
        public void sessionDestroyed(HttpSessionEvent se) {
            HttpSession session = se.getSession();
            HashMap<String, String> authsessions = 
                 (HashMap<String, String>) sc.getAttribute(AUTHSESSIONS);
            authsessions.remove(session.getId());
            sc.setAttribute(AUTHSESSIONS, authsessions);
        }
    
        public void attributeAdded(HttpSessionBindingEvent se) {
            if (USERNAME.equals(se.getName())) {
                HttpSession session = se.getSession();
                HashMap<String, String> authsessions = 
                      (HashMap<String, String>) sc.getAttribute(AUTHSESSIONS);
                authsessions.put(session.getId(), (String) se.getValue());
                sc.setAttribute(AUTHSESSIONS, authsessions);
            }
        }
    
        public void attributeRemoved(HttpSessionBindingEvent se) {}
    
        public void attributeReplaced(HttpSessionBindingEvent se) {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.