This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlet Date tracking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet Date tracking" Watch "servlet Date tracking" New topic
Author

servlet Date tracking

Tyrone Chandler
Greenhorn

Joined: Dec 05, 2003
Posts: 4
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!
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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

Joined: Jan 07, 1999
Posts: 6920
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

Joined: Jun 08, 2002
Posts: 3178
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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: servlet Date tracking
 
Similar Threads
Passed SCEA Part II/III with an 87%
Date conversion
showing system date in servlet
How can i get the first and the last date for a specific week
Date in a servlet