Results 1 to 5 of 5
  1. #1
    phalguner is offline Member
    Join Date
    Jul 2014
    Posts
    5
    Rep Power
    0

    Default Logout in Database on sessionTimeout/sessionInvalidate

    Hi,

    I am building a web application that cannot allow a user to be logged in on more than one computer at a time.

    To tackle this, I included a flag in the database which is set to 1 when the user is logged in and 0 if not.

    Right now, I am facing a problem when the user closes his browser window. I use ajax polls to the server every ten seconds or so. Also, the default session timeout is set at 1 minute. My ajax poll resets the session time every ten seconds as long as the browser window is open. Suppose that the user closes the browser window without logging out, then the poll does not reset the server timeout and after one minute the server invalidates the session.

    Could some one help me out with how I can also change the DB value to 0 (log out the user in the DB) once the session is invalidated?

    I use struts with jsp and an oracle database. Any answer using servlets is also welcome, I will migrate it.

    Thanks in advance,

    Phalgun

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Logout in Database on sessionTimeout/sessionInvalidate

    You might be able to use a session listener to listen for the session being destroyed to reset the flag.

    A simple HttpSessionListener example – active sessions counter

    Keep in mind that with these kind of solutions you -cannot- assume that your system will work all the time, you have to cater for a system failure that causes the flag to never be reset to zero and blocking the user forever. Next to your 0/1 value you could also store a last-activity timestamp and allow the user in X minutes after the last activity, even when the flag is set to 1.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    phalguner is offline Member
    Join Date
    Jul 2014
    Posts
    5
    Rep Power
    0

    Default Re: Logout in Database on sessionTimeout/sessionInvalidate

    You're right. I didn't think about that. I will now try this and see if it works.

    Thanks for the help!

  4. #4
    phalguner is offline Member
    Join Date
    Jul 2014
    Posts
    5
    Rep Power
    0

    Default Re: Logout in Database on sessionTimeout/sessionInvalidate

    W.R.T the example given:

    This listens to any sessions being created or destroyed. Now if I have to log out the user from the database when his session is destroyed, how will I be able to find out which user it is? His session might have just been destroyed. Is there any way to know?

  5. #5
    phalguner is offline Member
    Join Date
    Jul 2014
    Posts
    5
    Rep Power
    0

    Default Re: Logout in Database on sessionTimeout/sessionInvalidate

    It's alright. I got it.

    For anyone else, it is using the ' event' argument.

    event.getSession();

Similar Threads

  1. autosave in jsp when sessiontimeout
    By hemanth123 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-09-2011, 10:47 AM
  2. logout
    By mehak in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-07-2011, 10:40 AM
  3. Help for logout and cookie
    By Sweety13 in forum Java Servlet
    Replies: 2
    Last Post: 01-12-2011, 07:59 AM
  4. problem in logout...
    By raj.33jsr in forum Java Servlet
    Replies: 2
    Last Post: 08-09-2010, 12:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •