aspose file tools*
The moose likes Servlets and the fly likes making available a single session at any point of time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "making available a single session at any point of time" Watch "making available a single session at any point of time" New topic
Author

making available a single session at any point of time

paramesh ande
Greenhorn

Joined: Dec 15, 2005
Posts: 18
Hi,I want my web application to be accessible by only one client with a given username and password. If another client tries to access the application with the same username and password the previously logged in client should be logged out automatically and his session should be destroyed.
Can any body tell me possible solutions for this problem.FYI this web application is built by using servlets,jsps and the MVC model2 architecture,

Thanks in Advance
[ May 01, 2006: Message edited by: paramesh ande ]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Not sure but just guessing...

You must be having a table in database with all the usernames. Add one more boolean column (kind of flag: logged) in that table. Now set this column value to true for users who are logged in. And If another user is trying to login with the same username then check the flag first, if that flag is true (username is already in use) then do action whatever you want (restrict this user or logout previous user).

Any comments??
paramesh ande
Greenhorn

Joined: Dec 15, 2005
Posts: 18
Actually I am able to identify which user is logged in by using a hashtable which is declared as static.If any user logs in I will check the username in the hashtable, if it is there then it can be concluded that some other client is currently using that username.If it is not there i will add the username as an entry into the hashtable.
But the problem is how to invalidate the previous users session.Though I have the previous users request stored in the hashtable i could not use it.
Becoz that is alread responded(flushed), so it is giving null pointer exception.

Any how thank u very much for ur valuble suggession.
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
Originally posted by paramesh ande:
Though I have the previous users request stored in the hashtable i could not use it.Becoz that is alread responded(flushed), so it is giving null pointer exception.



Paramesh, I assume you are using username as key in Hashtable. Then why don't you store corresponding session as value rather than request. Then you will be able to get hold of session object and call invalidate() on it.

Regards,
Jass
paramesh ande
Greenhorn

Joined: Dec 15, 2005
Posts: 18
Singh actually I am storing a bean object which holds the request and response objects in the hashtable as value and username as key.
My idea is to send a message to the previous user regarding the new users login for that purpose I preserved the request response objects.But as the response already sent to that user these objects are getting nullified.
So i am not able send the message to the existing user.
As u said if i store the session itself then i can invalidate the session but the message could not be send.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
Paramesh, I assume you are using username as key in Hashtable. Then why don't you store corresponding session as value rather than request. Then you will be able to get hold of session object and call invalidate() on it.

This is not a good idea as the servlet container is responsible for session management. How would you clean up after sessions that the container has invalidated?
Why do you want to invalidate the already logged in user? Wouldn't it be more logical to tell the 2nd client that the username is already logged in?
Bill
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580

Why do you want to invalidate the already logged in user? Wouldn't it be more logical to tell the 2nd client that the username is already logged in?


Assume a scenario:

When a client(user) net connection is gone for some 1 (or) 2 minute and reconnects automatically.User may thought some problem and he clicks logout
(at this time net connection is not available and actual logout not taken place.Session invalidate in server not occur) and user open a new browser and login means he will get message already exists..its not good one in this case???
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: making available a single session at any point of time