aspose file tools*
The moose likes Servlets and the fly likes Online user Vs Offline Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Online user Vs Offline" Watch "Online user Vs Offline" New topic
Author

Online user Vs Offline

Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Hi all,

I have been wondering how to identify a user when he is online. What is the mechanism to use it.. I know I have to use HttpSession object for this. But i don't have the slightest idea about this..

My exact requirement is

"When a user is online, The status message in his profile will display online. The user is permissible to view other user profiles (in non-edit mode). When the user views other profiles, display must show whether other profile users are online or offline."

Thanks,
Abu.A
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The only mechanism you have to know whether someone is accessing your web application is a request. If the user makes a request they are "online". HTTP is stateless, so once that request is finished you can't really tell whether the user is still online, until they make another request. So you can probably pick an arbitrary number (say 5 minutes) and record who made a request in the last fime minutes. You could use the session for this; you may want a shorter check time than a usable session timeout would allow though. You also may want to add mechanisms to get a quicker response if they navigate away or close the browser, some sort of request fired on page unload should do that.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61773
    
  67

I think that the answer greatly depends upon what you define as "online".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
I think what Paul says, is happening in Javaranch forum. Because within a shorter time a user is asked to reauthenticate...
My guess is so..

Bear Bibeault wrote:I think that the answer greatly depends upon what you define as "online".


When a user is authenticated, He is free to navigate his profile as well as other profiles. He is free to modify his profile and only allowed to view other profiles. The time between the user authentication and user logoff is what i mentioned "online". User Logoff can also be automated through session timeout.

My requirement is User page must display the message "online" or "offline". Help me out.


Abu.A
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42951
    
  72
Help me out.

Paul's post contained a possible approach; if that's not workable in your case, tell us why.
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Guys, I got the idea of Paul. But I am asking you some of the ways to implement.

If i am going to use Paul guidelines, then each and every request is going to be checked before it serves its original purpose. That's what i was thinking about. For example,

An authenticated user is displayed online because he is constantly making requests. But when suddenly he clicks a link to some other profile. This request has to bring all the details about the other person's details like his name, profession, location etc. All the details can be fetched from database. Now I must also store the info about the person is online or offline. So that i can fetch the message whether he is online or offline to the viewing user.

I am asking the way to implement these. I think i am making my point here.


Abu.A
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42951
    
  72
You don't need to store the information whether someone is online in a DB. I'd use an in-memory Map<userID, lastAccessTimestamp>. You can have a background job that removes entries from this list if the timestamp is older than your definition of "online"; you could have that running every minute or so.
You also need to update that list every time a user accesses the site, so you need to put in place proper synchronization.
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90

Thanks buddy, i was sprinting around answers like this. Before i am cleared up.. How can i run the every-minute-or-so job in web app ( I mean standard way of doing this). I am wondering how can i run a code that's routinely checking for a particular interval. It must sound like Servlet Listeners....
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

A simple java.util.Timer will do.
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
So i can use util.Timer. Thats fine. Where can i write my task? I guess it must be in servlet context listener.
Guide me..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42951
    
  72
A context listener would indeed be a good place to set up both Timer and TimerTask.
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Thanks for all


ABU
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Online user Vs Offline