Knight_G
asked on
tracking online users
i am trying to track the users who are currently accesing my jsp web-app by changing the status of that user to 'online' in db when they login, then select all the users that have status 'online' to form a list.
when they logout, the status will change to 'offline' or something else.
but the problem is if the user do not click on logout but just close the internet explorer. the record in db haven't change to 'offline' yet.
how can i solve this without using session listener because i am using session for other purposes?
when they logout, the status will change to 'offline' or something else.
but the problem is if the user do not click on logout but just close the internet explorer. the record in db haven't change to 'offline' yet.
how can i solve this without using session listener because i am using session for other purposes?
Then, assuming you have something like:
<session-config>
<session-timeout>60</sessi on-timeout >
</session-config>
the sesson will time out after 60 minutes of user inactivity, and the user will be set to offline...
I cannot think of any other way of doing this...
> i am using session for other purposes
The session will still be destroyed on timeout....so you might as well take advantage of this :-)
Tim.
<session-config>
<session-timeout>60</sessi
</session-config>
the sesson will time out after 60 minutes of user inactivity, and the user will be set to offline...
I cannot think of any other way of doing this...
> i am using session for other purposes
The session will still be destroyed on timeout....so you might as well take advantage of this :-)
Tim.
One more option is to use onunload insdie your body tag of HTML.
This is not a 100% sure method but works pretty much okay for me..
in your HTML you have to put something like
<SCRIPT>
function myfunc()
{
window.open("yourlogouturl ","new');
}
<BODY onunload="myfunc()">
what this will do is will call the logout jsp or servlet even if user closes the browser by clicking at the cross on the top.... only downside is that it will pop up a new window after closing the original one..
Regards
This is not a 100% sure method but works pretty much okay for me..
in your HTML you have to put something like
<SCRIPT>
function myfunc()
{
window.open("yourlogouturl
}
<BODY onunload="myfunc()">
what this will do is will call the logout jsp or servlet even if user closes the browser by clicking at the cross on the top.... only downside is that it will pop up a new window after closing the original one..
Regards
Doesn't "onUnload" get called when you navigate off the page too?
Or am I wrong?
And is the event fired the same on both IE and NS?
If I'm right, I guess you could use frames, and have the unload handler in the main frameset definition page, rather than the frames themselves...
Just so I know :-)
Tim
Or am I wrong?
And is the event fired the same on both IE and NS?
If I'm right, I guess you could use frames, and have the unload handler in the main frameset definition page, rather than the frames themselves...
Just so I know :-)
Tim
Ooops...
Yup tim you are correct... It gets called whenever you get out of the page.... i.e. if page navigates to some other page...
But in my app I have two frames ( first frame holds all the all time available functions which includes logout ofcourse) and my rest of the app navigates in second frame so I never happen to see the behaviour....:-)
and as I say its not a 100% sure shot method... it works fine for IE and netscape doesn't like it..:-)
and I guess in your above comment you are reffering to the same case as mine ( uses of frames....)
thanx for correcting me....:-)
Yup tim you are correct... It gets called whenever you get out of the page.... i.e. if page navigates to some other page...
But in my app I have two frames ( first frame holds all the all time available functions which includes logout ofcourse) and my rest of the app navigates in second frame so I never happen to see the behaviour....:-)
and as I say its not a 100% sure shot method... it works fine for IE and netscape doesn't like it..:-)
and I guess in your above comment you are reffering to the same case as mine ( uses of frames....)
thanx for correcting me....:-)
:-)
Heh, I wasn't sure ... I always do things like this via trial and error ;-)
Heh, I wasn't sure ... I always do things like this via trial and error ;-)
ASKER
TimYates:
thanx, let me try on ur code...
thanx, let me try on ur code...
It does have the side-effect that users who have closed their browser will not be marked as "off-line" until the session times out (so it won't be immediate)
For immediate log out, the only way is to use Kuldeepchaturvedi's method of frames, and onunload javascript in the main frame's html
For immediate log out, the only way is to use Kuldeepchaturvedi's method of frames, and onunload javascript in the main frame's html
ASKER
i am using tomcat 3.2, is there any problem if i want to use session listener?
because i got "class not found" error
because i got "class not found" error
HttpSessionListener requires Tomcat 4 . If you don't want to upgrade then you could use the HttpSessionBindingListener interface.
>changing the status of that user to 'online' in db when they login
You could just create a context attribute object. That object could be a Vector. In a userbean, that implements the HttpSessionBindingListener interface, you could add the user to that Vector in its valueBound method. You could remove the user in its valueUnbound method.
Also, look at the setMaxInactiveInterval(int ) method of HttpSession for more control over when you bump user off.
>changing the status of that user to 'online' in db when they login
You could just create a context attribute object. That object could be a Vector. In a userbean, that implements the HttpSessionBindingListener
Also, look at the setMaxInactiveInterval(int
ASKER
sorry i'm new to jsp.
session.invalidate() will call the valueUnbound() method, am i right?
but how about when i close my broswer directly? will it call valueUnbound()?
session.invalidate() will call the valueUnbound() method, am i right?
but how about when i close my broswer directly? will it call valueUnbound()?
It will call valueUnbound() after the session timeout period if user closes the browser directly... i.e. if user closes the browser and your session is left danglling, it will call invalidate after the timeout period of session ( default for tomcat is 30 mins I guess).... so this method will be called but after a delay of 30 mins if user closes the browser directly.
ASKER
ooh, that means although the user had left the app, it must wait for 30mins before the status is change back to offline in db. but it would lead to inaccuracy in between this 30mins :(
is there any better solution? will the HttpSessionListener sessionDetroy() had the same problem?
anyway, thanks for ur point
is there any better solution? will the HttpSessionListener sessionDetroy() had the same problem?
anyway, thanks for ur point
Well thats true...
now only way may be to use my suggestion of using frames and unLoad function... but it may not be a feasible solution for NS and you may also have to introduce frames in your system if its not there already...
Regards
now only way may be to use my suggestion of using frames and unLoad function... but it may not be a feasible solution for NS and you may also have to introduce frames in your system if its not there already...
Regards
ASKER
unfornuately, my app contain several same window(ie, each window will handle different record) the user must close all the window to indicate as logoff.
if onUnload was used, when the user just close a window, then it will indicate as logoff, but it is not true while there are some other window opening.
thats why i can't use onUnload :(
if onUnload was used, when the user just close a window, then it will indicate as logoff, but it is not true while there are some other window opening.
thats why i can't use onUnload :(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ooooh, yeah
based on the concept of of hidden frame, i have modified a bit to suit my app,
actually my app has no parent window, each pop-up window is the same. if unload event used, then every window will include this event, if user click on either one, the system will logged off. this is not what i want.
my hidden frame will refresh within a time duration(60 sec) and save a timestamp to a table. for user tracking page, it will select all records that have time now()-timestamp < 60 sec.
that means if the user close all the window, then no hidden frame will refresh the table, the time will be expired soon.
thanks for kuldeep idea.
based on the concept of of hidden frame, i have modified a bit to suit my app,
actually my app has no parent window, each pop-up window is the same. if unload event used, then every window will include this event, if user click on either one, the system will logged off. this is not what i want.
my hidden frame will refresh within a time duration(60 sec) and save a timestamp to a table. for user tracking page, it will select all records that have time now()-timestamp < 60 sec.
that means if the user close all the window, then no hidden frame will refresh the table, the time will be expired soon.
thanks for kuldeep idea.
glad to be of any help.....:-)
You can listen for the session being destroyed (time-out), and then remove that user from the online list (or set their online status to false)
That is the only way I can think of for doing it...as they never click logoff :-(
Something like:
--------------
import javax.servlet.http.* ;
import org.picr.vice.wrappers.* ;
import java.util.* ;
public class MySessionFilter implements HttpSessionListener
{
public void sessionCreated(HttpSession
public void sessionDestroyed(HttpSessi
{
// set to offline in the db (if not already)
}
}
-------------
then:
<listener>
<listener-class>MySessionF
</listener>
in web.xml