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
posted
0
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
posted
0
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
posted
0
"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.