This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Servlets and the fly likes Can i distinguish between log out or session timeout? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Can i distinguish between log out or session timeout?" Watch "Can i distinguish between log out or session timeout?" New topic
Author

Can i distinguish between log out or session timeout?

harry lee
Greenhorn

Joined: Jun 02, 2006
Posts: 5
Hi all,
Can i distinguish between the log out or the session timeout?By this,i can insert session timeout infomation into log table.When it is user log out ,i can insert "user log out" int to log table.So,can i use listener to make this work?
Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56529
    
  14

That greatly depends on what you mean by "logging out".


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Assuming that log out means you user clicks on the logout link provided in the frontend.

In this case case you explicitly call session's invalidate method and once this call is done the sessionDestroyed method of HttpSessionListener is called.
In case of timeout this is not the case and the HttpSessionListener's sessionDestroyed is called directly on timeout.

The possible solution i see here is to write to db before calling the invalidate method for logout and in sessionDestroyed check if any entry was made in db, if not its a session timeout.
harry lee
Greenhorn

Joined: Jun 02, 2006
Posts: 5
Haha,i know now ,Thanks.
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
just a passing thought.

are you trying to simulate the situation where a user keeps his browser open for a long time, and then clicks on link
and sees
"Sorry your session has timed out"
and in cases he has explicitly logged out, he sees the login page?

one way I see to achieve this is-
in the first case, the browser sends in a JSESSIONID in the cookie header that refers to a nonexistent session. therefore getSession(false) would return null
in the second case, neither is such a header sent, nor is the session available.
if i am not mistaken that is...
i might be missing something here- haven't thought on these lines for a long time.
so you could probably insert a filter that does this check.


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

you may have a flag which indicates weather a user has logged out or not.
you can set this flag when user clicks on logout link.
in the case of session timeout this flag will not be set so you can figure out that its session time out.

What you can do is, when there is no session exists for a user, you can check if the flag is set, if its set than it means user had successfully logged out last time. if it is not it indicated session time out.
you can write a filter for that.


[Servlet tutorial] [Servlet 3.0 Cook Book]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Can i distinguish between log out or session timeout?
 
Similar Threads
Restrict Login from Multiple Sessions
How to check browser alive
HELP!!!
Handling session timeout and session invalidate differently
Session timeouts and listeners....