Results 1 to 15 of 15
  1. #1
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default [SOLVED] logout user when session expires

    Hi All,

    I have developed a web application. In which i want to add logout functionality. For this i used HttpSessionListener, but not working as i wanted.

    I marked user logout in sessionDestroyed() method(by updating Database).

    If session is timeout then container calls the sessionDestroyed() method and i update the Database but when i explicitly call session.invalidate() (when user click on logout link)values are unbind from session object but container does not call sessionDestroyed() method so not able to update database.

    and also need to logout user when user closes browser window.


    Please give your valuable idea on the same.

    sanjeev

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hi Dear all,

    I got the solution itself.:)

    Thanks for your time.


    sanjeev

  3. #3
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Hei Sanjeev Can you please explain how you did it?

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default


    I implemented HttpSessionListener interface and there are two methods

    sessionDestroyed() and sessionCreated(). Nothing to do with session created.
    I just updated database in sessionDestroyed() method, and i will be called by container just before session.invalidate() is called.

    and added the listener entry into web.xml file.

    There are three chances from where user can be marked as logout.
    1. Session timeout by container.
    2. User clicks on Logout link
    3 User close the browser window.

    In First case Listener is called by Container and Database is updated.
    In second case i called a servlet that do the session.invalidate() and calles a method to update Database.
    In 3rd case : When user closes the browser window i called the same servlet from scripts and updated the Database


    If still you have any doubts the please feel free to ask anything.


    sanjeev

  5. #5
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Hei Sanjeev,
    My doubt is why are you updating each time the database

    when Session Expires? Can You Please Explain?

  6. #6
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Not each time dear....just one time from any one of three condition....for particular one user.


    sanjeev

  7. #7
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Ok,
    That means you are tracking active users of app and storing to db?

  8. #8
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Ya exactly,


    we need to track only users who are logged in.


    sanejev

  9. #9
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    OK sanjeev,
    Thank you.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Sanjeev, I have one question.

    You are talking about three chances which can be caused to user logout. Fine. How did you check the port. Is there any service is implemented by you to check that port is available.

    It can be a cause, if the user have firewall used, isn't it?

  11. #11
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hi Eranga,

    How did you check the port. Is there any service is implemented by you to check that port is available.....?

    Why i should check for port..?

    If i am wrong or there is another way from user can be logged out please correct me.


    sanjeev

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sanjeevtarar View Post
    If i am wrong or there is another way from user can be logged out please correct me.
    No Snjeev, I'm not talking about user logged out in the normal way. What I mean there is, if the large number of user are online/available at the same time, there can be a clash in the communicating port. As a result port can be closed. So it can be a real advantage to check the port periodically.

    Why I'm asking this is, using Java I never do this. All time I used .Net services fro that. It's so simple. :)

  13. #13
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default Urgent

    Hi......
    I've one doubt ,
    u said that while closing browser u r calling some servlet,
    but u didn't mentioned that which browser u r using.

    If u are using Mozilla please tell me the steps u followed to call the servlet while closing the browser.

    Thanks

    SRIKANTH:)

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

    Default Re: [SOLVED] logout user when session expires

    Hi, Sanjeev.


    Could you please help me with the 3rd point. I want to update the users record in the database when he/she closes the browser window.

    For now, I call the server using ajax polling every ten seconds which resets the sessionTimeout. Suppose the user closes the browser window without logging out, then the server does not reset the sessionTimeout and then it automatically invalidates the session after a set period of time. What I want to do is to also log out the user in the database after the session has timed out.

    I use struts but if you help me using even jsp I migrate it.

    Thanks in advance,
    Phalgun

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: [SOLVED] logout user when session expires

    Come on, this is 6 years old!

    I'm locking the thread.
    If you have a question then start a new thread.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Need to return to login page once logout
    By peiceonly in forum Suggestions & Feedback
    Replies: 2
    Last Post: 03-31-2009, 08:50 PM
  2. Logout problem
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 01-09-2008, 08:54 AM
  3. logout using java script
    By pankajagar2001 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-03-2008, 07:37 AM
  4. Sessions and logout question
    By orchid in forum Web Frameworks
    Replies: 1
    Last Post: 05-18-2007, 07:24 PM

Posting Permissions

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