Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Track the user session list

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Track the user session list

    How do we have a track of users who have logged in? I want to have a table of record with usernames and the status(logged in or logged out).

    Whenever the user logs in, the status shud indicate he is logged in and when he logs out the status shud change accordingly. Does anybody know how to do this???


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Track the user session list

    I take it you are handling the login/logout your self, as in looking up the user credentials from somewhere and matching them with the user input.

    Just have it so that whenever a login is performed you add the user to a list of logged in users, maybe a static list somewhere like a SessionStatistics class or whatever.

    Whenever a user logs out you remove the user from the list. Better make the list a map I guess.

    However if you also which to make sure that whenever a session is destroyed by the application server you can have your session manager implement HttpSessionListener.

    Here is a quick example.

    public class SessionManager implements HttpSessionListener {
     
            @Override
    	public void sessionCreated(HttpSessionEvent sessionEvent) {
                // This is called whenever a new session is created, no matter if the user is logged in or not
    	}
     
            @Override
    	public void sessionDestroyed(HttpSessionEvent sessionEvent) {
                // This is called whenever a session is destroyed
                // In here you can have some code that gets the session off the sessionEvent object and then checks to see if this session represents a user who was logged in. Since the session is now about to be destroyed you can then safely remove that user from your map/list of logged in users.
            }
    }

    Of course you also need to add this listener in your web.xml file.

        <listener>
            <listener-class>my.package.name.SessionManager</listener-class>
        </listener>

    Hope that makes any sense. If you need more help in looking at the specific code for adding/removing the user from the map/list, let us know and I shall see what I can do.

    // Json
    Last edited by Json; October 23rd, 2009 at 02:54 AM.

  3. The Following User Says Thank You to Json For This Useful Post:

    kalees (October 23rd, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Track the user session list

    public class SessionManager implements HttpSessionListener {
     
            @Override
    	public void sessionCreated(HttpSessionEvent sessionEvent) {
                // This is called whenever a new session is created, no matter if the user is logged in or not
    	}
     
            @Override
    	public void sessionDestroyed(HttpSessionEvent sessionEvent) {
                // This is called whenever a session is destroyed
                // In here you can have some code that gets the session off the sessionEvent object and then checks to see if this session represents a user who was logged in. Since the session is now about to be destroyed you can then safely remove that user from your map/list of logged in users.
            }
    }

    I am having a doubt,i don't know how to get the name of the user using sessionCreated method,...can u pls give me some more code for sessionCreated method!....if u don't mind

  5. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Track the user session list

    Well sessionCreated is called at the first instance the users session is created, the first time they hit your web application. I dont think you can do anything in there to add the user to a list if you only want to track logged in users.

    You need to add a method of your own which adds a user to the list when they have logged in.

    However in the method sessionDestroyed you can remove the user from the list.

    // Json

Similar Threads

  1. Java session problem
    By Padmaja in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 5th, 2009, 09:06 PM
  2. Session Timeout in LDAP
    By retail_deepa in forum Java Servlet
    Replies: 0
    Last Post: August 4th, 2009, 03:26 AM
  3. How can i put session in Javascript?
    By rajani in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 8th, 2009, 12:46 PM
  4. opening Telnet Command Session
    By voyager in forum Java Networking
    Replies: 3
    Last Post: June 23rd, 2009, 10:34 AM