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
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:
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:
Recommend the tutorial