Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Single log in  RSS feed

 
sindhu sheela
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I would like to implement single login for a given login id and password.
This is implemented by setting up a flag in the database.which when logged in will be set "1" and when logged out will be set back to "0".I'm just wondering how will one know if the user just closes the window not by logging off .

Can any one tel me how this can be implemented!

Thanks in advance.
 
ujjwal soni
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This can be easily done with couple of javascript and ajax calls.

Check out the code below:::

Here is Ajax that will make a request to the Logout.jsp.





MyJsp.jsp



I hope this makes things clear...

>
 
sindhu sheela
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply .
Will implement the same , get back if got any problem.
 
ujjwal soni
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok....
 
Charbel Keyrouz
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got some concerns about the suggested solution since the onbeforeunload will be triggered even if the user presses F5.
If the form is submitted this event will be triggered, I do not think this is the ideal solution to handle this issue although it works some times.

The best solution is to implement a session listener servlet and initialize it in the web.xml.
The purpose of this session listener is to do an action just before the session ends.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The proposed solution will not work just for that reason.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36463
462
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sindhu sheela wrote:I'm just wondering how will one know if the user just closes the window not by logging off .

This is a valid case. If I close the window and then login in a new one, I want the new one to be the active session and the original invalidated. I use one product that tells me I have another session open and gives me the choice of logging out the other session or deciding not to login.
 
Charbel Keyrouz
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try pressing on F5, then the session will end.
 
sindhu sheela
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thanks all for the reply.
Hey Charbel , yeah i could not use the onbeforeunload , as it would be triggered on either refresh or if I'm navigating between the menu's.
I have no awareness about Session Listener Servlet .Can i get some reference links ,that would be very helpful.


Thanks again
 
Charbel Keyrouz
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go to this url : http://wiki.metawerx.net/wiki/SessionListener
and you will find a sample of it.

Note that you should write your class and initialize it in the web.xml file like this:
<listener>
<listener-class>MySessionListener</listener-class>
</listener>
(it is mentioned in the url as well).

There are two methods that you should use:
sessionCreated and sessionDestroyed.

These methods are inherited from the interface HttpSessionListener.

In your case you should put your code that sets the flag to 0 in the method sessionDestroyed.

The call of this method only occurs when the session is being finalized.
On window close for example.

If you need any further assistance do not hesitate.
 
sindhu sheela
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Charbel thanks for the reply again,
I immplemented the same.It was working fine either when the user clicks on log out or when the session is timed out.But it did not happen the same when the user clicks on the x button of the browser.
I mean the sessionDestroy method was not called onClick of x button(close button) of the broswer.
How do i go about it now?



 
Charbel Keyrouz
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have checked something but I think the closing of the window should be handled client side and the session listener does not grabs the event.
Anyway I found a way by adding this to your javascript code:
 
sindhu sheela
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Charbel ,
Thanks it worked.
I had another issue.please have a look at the below link
http://www.coderanch.com/t/450075/JSP/java/Single-login-Particular-url#2003020

hoping for help regarding the same.

Thanks again,
 
sindhu sheela
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Charbel,

The above is working fine for window close(onclick of x button ).as the logic checks for XClient and Yclient co-ordinates,the log out is called even if i click on refresh button.
Im using IE 7 browser.How do i go about it?

I have few more condition,which i had failed to check initially.
1.It should log off when the browser is minimized,user right clicks on it and then click on close option.
2.One can change the window size i then click on x-button.
3.One can click on Menu Option File-Exit.

Thanks in advance.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!