TrainBeaser for iPhone
The moose likes Servlets and the fly likes Need help on session/time Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Need help on session/time" Watch "Need help on session/time" New topic
Author

Need help on session/time

Michael Yang
Greenhorn

Joined: Mar 04, 2001
Posts: 3
Hi all,
I am a newbie trying to get my feet wet in the vast world of Jsp/Servlets. I am trying to create a servlet based web application. One of the requirement is to find a way to indicate the users how long since his/her session started. How do I go about solving this problem??
Thank you very much
sincerely,
Michael Yang
Milan
Greenhorn

Joined: Mar 05, 2001
Posts: 5
Hello,
There are the following methods available for session.The whole idea is that you Get the Time (throgh Date) when the Session was started and then compare it with the present time. Calculate the difference and display it to the User.
Things will be clear with these codes:
import java.util.*;//for Date Class
:
:
:
//within the Releveant methood (i.e doGet or doPost)
:
Date startSes=new Date(ses.getCreationTime());
: //ses is the Object of Httpsession
Date currSes=new Date(ses.getLastAccessedTime());
:
://Now Compare
long l1=startSes.getTime();//in milliseconds
long l2=currSes.getTime();
:
pw.println("The time elapsed since your Session started is" +(l1-l2)/1000 +"secs");
:
Hope this is some help to you,
Regards,
Milan Doshi

Milan Doshi
Michael Yang
Greenhorn

Joined: Mar 04, 2001
Posts: 3
Thanks a lot Milan, for your help. I was thinking about writing an applet to display the session time, so it can be refreshed and looked just like a digital clock. I still haven't find a way to call the applet from my servlet. I will give your code a try and see what happened.
Thank you very much
Michael
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
"Milan",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Posts which contravene the naming convention are not eligible to win books! Please choose a new name which meets the requirements.

Thanks.


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
 
 
subject: Need help on session/time
 
Threads others viewed
EJB3 testing with JUnit4
Session gets destroyed when invoking application of another server in a same browser
Session data
Disabling a previous session in a web app
Httpsession and user session timeout tracking.
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture