This example illustrates to find current access time of session and last access time of session. Sessions are used to maintain state and user identity across multiple page requests. An implementation of HttpSession represents the server's view of the session. The server considers a session to be new until it has been joined by the client. Until the client joins the session, isNew() method returns true.
Here is the source code of LastAccessTime.java
import java.io.*;
|
Description of code: In the above servlet, isNew() method is used to find whether session is new or old. The getCreationTime() method is used to find the time when session was created. The getLastAccessedTime() method is used to find when last time session was accessed by the user.
Here is the mapping of servlet ("LastAccessTime.java") in the web.xml file:
| <servlet> <servlet-name>LastAccessTime</servlet-name> <servlet-class>LastAccessTime</servlet-class> </servlet> <servlet-mapping> <servlet-name>LastAccessTime</servlet-name> <url-pattern>/LastAccessTime</url-pattern> </servlet-mapping> |
Running the servlet by this url: http://localhost:8080/CodingDiaryExample/LastAccessTimedisplays the output like below:
When user re-calls the servlet the creation time will be same but last accessed time will be changed as shown in the following figure:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Session Last Accessed Time Example View All Comments
Post your Comment