• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help on session/time

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
"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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic