aspose file tools*
The moose likes Servlets and the fly likes Getting active session from the session id Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting active session from the session id " Watch "Getting active session from the session id " New topic
Author

Getting active session from the session id

Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Referring the this post Invalidating all sessions

I have stored all the active sessions in a database by sessionid and the unique username within the organization.

When I want to do an update or a shutdown of the webapp. I want to inform my users of that so that they can save their work.

So from the sessionid which is stored in the database I want to send a message to the user.

An another way of thinking is that I could send a general message to all users in the form of an variable on application scope, but I'm still curious how to connect directly to a user session if needed.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

First, out of curiosity, are you removing the session IDs from the database when the sessions are invalidated?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you bind a map to application scope, and then, as sesssions are created, using a session listener, bind the session to that map, using the session id as the key, you can, at any time iterate over the map and get a handle to each session.

Make sure you remove the sessions from the map when they are invalidated.
Sundaram Karthick
Greenhorn

Joined: Jun 26, 2007
Posts: 24
session listner is a good option. i have a question here out of curiosity, how are you planning to push data to the users? do you use some applet in client side?


SCJP 1.5, SCWCD 1.4, SCBCD 5
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Ben,

I understand that you're suggesting to keep a map of active sessions which I could use for getting the desired session. This might work. I've already code for updating the database. I could change this code.

I'm not removing the id's. I update the database table where activeInd is set to 0. Records older then a month are removed automatically.

When I have a logged error and a certain time I'm able to see what kind of users where logged into the system. Al those errors and special events are also logged into the system. I use this data of customers for finding problems which could be solved. Mostly in this way I'm able to solve a problem before my customer discovers the problem which is always better. You probably already know a lot of problems could arise with multi threading.

Now I want to implement a nice updating system in which the users will get a warning. when a update will take place and I want to close all the sessions gracefully, because I had a few incidents where there was a corruption in the business logic because a session was shutdown.

I know I have to make my application better for circumventing this kind of issues, but you can always try not to have them. I now the server could shutdown, network failure, database closure, etc.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you bind a reference to a context scoped object (Map), it is important that you remove it when the session expires or you will end up with a memory leak.

There is an example on my site.
http://simple.souther.us/not-so-simple.html that probably does most of what you want (as far as managing references to sessions).
Look for SessionManager.

Since HTTP is stateless, you can't really push a message to a user but, since you've got a reference to their session, you could bind a message to it.
Then, the next time the user makes a request you could either show them the message on the current page or redirect them to a page with the message on it.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Ben,

Thanks,

I've bookmarked your site.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting active session from the session id
 
Similar Threads
Ending a "specific" session
Single reference between Instances
Get previous session with the Session Id
HttpSession
URL rewriting?