Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help on session/time

 
Michael Yang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Yang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic