get list of open session

Hello, if i get sessions like

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws ServletException{

request.getSession().removeAttribute("web_user");

}

is there is a way to find, list of open sessions, or list of ppl who loged into the system?

[481 byte] By [Dilana] at [2007-10-2 5:12:28]
# 1

A session is associated to a user. Removing an attribute from a session will have no impact on other sessions. You will have to store the currently active users in a higher scope (one thats common to all users - ServletContext is a good choice).

You will also have to use a combination of listeners to achieve this.

Assume you have a user object bound to each session (that stores the logged in user info).

some login process inside, say a Servlet

-

User userObj = new User();

//userObj.setXXX() setter methods that store user data in this object

request.getSession.setAttribute("web_user", userObj);

Step 1.

-

Create a list to hold all active users in your context object. This list is instantiated when the context fires.

public class CtxListener implements ServletContextListener{

public void contextInitialized(ServletContextEvent ctxEvent){

ctxEvent.getServletContext().setAttribute("currentUsers", new ArrayList());

}

public void contextDestroyed(ServletContextEvent sce){}

}

Step 2

Use a HttpSessionBindingListener that informs you when a UserObject is bound to a session.

public class User implements HttpSessionBindingListener {

//variables and get/set methods go here

public void valueBound(HttpSessionBindingEvent event){

//get a handle to the context page

ServletContext ctx = event.getSession().getServletContext();

//get a handle to the user's list ctxt attribute

List userList = (List)ctx.getAttribute("currentUsers");

//add this object to the user list

userList.add(this);

}

public void valueUnbound(HttpSessionBindingEvent event){

//do nothing

//we will remove logged out users in a different manner

}

}

Step 3

Now at any point where you have access to the ServletContext(which is virtually everywhere in a web application), you can get the list of users in the following fashion.

//some servlet

{

List currentUsers = (List)getServletContext.getAttribute("currentUsers")

}

Step 4

We have to remove logged out users. Do it using a SessionListener

public class SessionListener implements HttpSessionListener{

public void sessionCreated(HttpSessionEvent se){

}

public void sessionDestroyed(HttpSessionEvent se){

HttpSession session = event.getSession();

ServletContext ctx = session.getServletContext();

List currentUsers = (List)ctx.getAttribute("currentUsers");

currentUsers.remove(session.getAttribute("web_user"));

}

}

You should ofcourse register your listeners in web.xml

Cheers,

Ram.

Madathil_Prasada at 2007-7-16 1:15:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for the information,

how does it get to work,

well i have three classes, one for

HttpSessionListener - SessionListener()

ServletContextListener - CtxListener()

HttpSessionBindingListener - UserListener()

for the 3 steps, and put them into a web.xml as well,

<!-- Listeners -->

<listener>

<listener-class>com.web.util.session.SessionListener</listener-class>

</listener>

<listener>

<listener-class>com.web.util.session.CtxListener</listener-class>

</listener>

<listener>

<listener-class>com.web.util.session.UserListener</listener-class>

</listener>

how how does it add the users?

and i have

loginInAction {

..

// user is the , user object

request.getSession().setAttribute("web_user", user);

}

i did that, but its not adding, i get list of empty array of usrs. when i do the listing, on jsp as;

List currentUser = (List)getServletContext().getAttribute("currentUsers");

<%= currentUser.size() %>

Dilana at 2007-7-16 1:15:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
The UserListener should ofcourse be the User class that's added to the session.If you have done that, I would suggest you put debug statements in your code and find out if the listeners are being fired.ram.
Madathil_Prasada at 2007-7-16 1:15:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hello,

I change a code around;

this is what i got

public class UserTracker implements HttpSessionBindingListener {

private static UserTracker instance = null;

static List _activeUsers = new ArrayList();

public UserTracker() {

super();

}

public static List getUserTracker() { return _activeUsers; }

public static void setUserTracker(List activeUsers) { _activeUsers = activeUsers; }

public static UserTracker getInstance() {

if (instance == null) {

instance = new UserTracker();

}

return instance;

}

public void valueBound(HttpSessionBindingEvent event) {

_activeUsers.add(event.getSession().getAttribute("_user"));

}

public void valueUnbound(HttpSessionBindingEvent event) {

//_activeUsers.remove(event.getSession().getAttribute("_user"));

}

}

and

public class UserTrackerRemove implements HttpSessionListener{

private static Logger logger = Logger.getLogger(UserTrackerRemove.class);

public void sessionCreated(HttpSessionEvent se){

}

public void sessionDestroyed(HttpSessionEvent se){

HttpSession session = se.getSession();

ServletContext ctx = session.getServletContext();

List currentUsers = UserTracker.getUserTracker();

currentUsers.remove(session.getAttribute("_user"));

UserTracker.setUserTracker(currentUsers);

}

}

and put

login {

..

request.getSession().setAttribute("_user", user);

request.getSession().setAttribute("tracker", UserTracker.getInstance());

..

}

and put them both on xml as

<!-- Listeners -->

<listener>

<listener-class>com.web.util.UserTracker</listener-class>

</listener>

<listener>

<listener-class>com.web.util.UserTrackerRemove</listener-class>

</listener>

and now its works; i get the list as

List currentUser = UserTracker.getUserTracker();

but when user loged out or timed out, user do not remove from the list, why is that, where did i went wrong? :)

Dilana at 2007-7-16 1:15:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hello, can some one able to help me,

i just cant get to work the HttpSessionListener,

i put few debug statements within the

public class UserTrackerRemove implements HttpSessionListener{

private static Logger logger = Logger.getLogger(UserTrackerRemove.class);

public void sessionCreated(HttpSessionEvent se){

}

public void sessionDestroyed(HttpSessionEvent se){

HttpSession session = se.getSession();

ServletContext ctx = session.getServletContext();

List currentUsers = UserTracker.getUserTracker();

currentUsers.remove(session.getAttribute("_user"));

UserTracker.setUserTracker(currentUsers);

}

}

but its not running, when i log out, do i have to call it in some other way?

Dilana at 2007-7-16 1:15:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Hi,

I had the same struggle. I notice that when a session is destroyed its impossible to retrieve the session attribute. The only thing which is still available is the session ID. That is why I used a Hashtable with a session ID as key. Now I can manage the userList.

public void valueBound(HttpSessionBindingEvent event){

//get a handle to the context page

ServletContext ctx = event.getSession().getServletContext();

//get a handle to the user's list ctxt attribute

Hashtable userList = (Hashtable)ctx.getAttribute("currentUsers");

//add this object to the user list

userList.put(event.getSession().getId(),this);

}

public void valueUnbound(HttpSessionBindingEvent event){

ServletContext ctx = event.getSession().getServletContext();

Hashtable userList = (Hashtable)ctx.getAttribute("currentUsers");

//remove this object to the user list

userList.remove(event.getSession().getId());

}

Oostervelda at 2007-7-16 1:15:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Check out this for example: http://www.servletsuite.com/servlets/sessadm.htmyou can get access to he sessions from JSP file too
dnamiota at 2007-7-16 1:15:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...