• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

servlet Date tracking

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I wanted to know if anyone could tell me how I can put a Date or Calender object in a servlet so that it tells me how long it's been since i last visited a page? Basically I'm looking to tell the visitor the number of seconds since the last visit. Thanks so much!
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh well... U have to maintain the users status in external database to do that... Fisrt u have to import the java.util.Calendar, develop a class that extends Calendar, implement HttpSessionBindingListener... You have to know how to do things in valueBound and valueUnbound methods of that interface(Save the date and time to the database in valueUnbound method)... Instantiate one object of them in your servlet and add to the current live session... If the session is invalidated, the valueUnbound method of your class will be called...
Actually if I believe that what I've mentioned above is not enough to do real implementation... U might need to study about the listener and session management more....
Anyway, it might helps you logically...
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds an overly-complicated solution to me.
To solve this issue in a simpler way, first you need to decide what constitutes the "I" in "since I last visited the page". Have you already logged on or otherwise authenticated yourself? Is the IP address of your computer enought to identify you?
Once you have something which identifies you to the computer, the simplest way of maintaining "last accessed" information is to place a Map (such as a HashMap) in the "application" context, keyed by your used id, with each entry being a Date object.
For example, in your servlet you might have code like:


This should work for as long as your web application is running, but the data will be lost of the application or the server stops or restarts. For a more permanent solution, you need to persist this information, typically to a file or database.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Carver:
That sounds an overly-complicated solution to me.


I'm trying to explain... But I would be sorry, if my words make the readers complicated..... But the concept is to be notified, when a session is invalidated and save the logout time into the database...
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when a session is invalidated and save the logout time into the database
Perhaps we have different understanbdings of what theoriginal poster was asking for. I read "how long it's been since i last visited a page" as a simple need to store the last time each user visited a page. Note that this is not the same as "how long it's been since i last visited a site".
This has nothing to do with sessions - If I visit a page now, and again in two minutes time, I will still have the same session, but I want the "last accessed" data to be updated each time.
If the map of last visit dates needs to be persisted, it might either be persisted every time a date is updated using a DB, a prevalence layer, etc.), or on a scheduled basis (say once per hour or whatever).
Can you explain why you feel that session creation and expiry might affect this problem?
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Carver:
[QBPerhaps we have different understanbdings of what theoriginal poster was asking for. I read "how long it's been since i last visited a page" as a simple need to store the last time each user visited a page. Note that this is not the same as "how long it's been since i last visited a site".
If the map of last visit dates needs to be persisted, it might either be persisted every time a date is updated using a DB, a prevalence layer, etc.), or on a scheduled basis (say once per hour or whatever).
Can you explain why you feel that session creation and expiry might affect this problem?[/QB]


Yeah, I also think that I misinterpreted the original post..... I thought that the user wants to view the last time that he/she logged into the system... So I let the HttpSessionBindingListener to notify the event to the object, when his/her session is invalidated...
Of course, I now know that I misinterpret the post...
I'm sorry, Mr.Frank...
 
reply
    Bookmark Topic Watch Topic
  • New Topic