Using Session+application+cookie to realize single-state login and solve the problem of unexpected browser shutdown

Source: Internet
Author: User

Recently in helping a friend to make a shopping site, which involves the issue of landing. These include the login of the background administrator and the user's login.

In this site, the design of the background administrator single-state login, that is, an account can only have a login instance. It is easy to think of is implemented with application, put a hashmap in the application, store the login administrator information. At the same time, in order to achieve the login timeout, also in the session to store the landing object, by setting the session of the listener monitor the demise of the session, remove the objects in the application. Then the problem comes, if the user because of power loss or willful, illegal shut down the browser, and then open the browser, it is obvious that the application in the last landing object, can not be landed again, so that the user can never log on the problem.

Check on the Internet, a lot of people encounter this problem, omnipotent netizens to the proposal is to use the way of heartbeat package, Judge users offline, but I think this method is too occupation of resources. So here's the method. The core idea is to use a cookie to save the successfully logged-in SessionID, and then add a Hashmap,key to the Application object that is Sessionid,value still an administrator object. Each time you open the login screen, first check the cookie, if it exists to remove the SessionID, and then see the application save SessionID HashMap, if there is this sessionid, take out the administrator information, And put it in a new session.

PS: The browser session mechanism is this, each time the browser server opens a new session for the browser, its disappearance is not controlled by the browser's close. Instead, the server is automatically recycled, but if the browser closes and then opens, a new session is created, but does not mean that the server cannot find the last session that opened the browser. So storing sessionid in a cookie is equivalent to finding the last session in the browser and manipulating it.

The source code is as follows

(1) Session Listener code


[Java]View PlainCopy
  1. ServletContext Application;
  2. @Override
  3. public void sessioncreated (httpsessionevent arg0)
  4. {
  5. ServletContext application=arg0.getsession (). Getservletcontext ();
  6. Hashmap<string,managerform>allmanagerbysessionid= (hashmap<string,managerform>)  Application.getattribute ("Allmanagerbysessionid");
  7. if (null==allmanagerbysessionid)
  8. {
  9. allmanagerbysessionid=new hashmap<string,managerform> ();
  10. Application.setattribute ("Allmanagerbysessionid", Allmanagerbysessionid);
  11. }
  12. System.out.println ("create");
  13. }
  14. @SuppressWarnings ("unchecked")
  15. @Override
  16. public void sessiondestroyed (httpsessionevent arg0)
  17. {
  18. Managerform form = (managerform) arg0.getsession (). getattribute ("user");
  19. Application=arg0.getsession (). Getservletcontext ();
  20. Hashmap<string, managerform>hashmap= (hashmap<string, managerform>) Application.getattribute ("  Allmanager ");
  21. if (hashmap!=null)
  22. {
  23. if (null!= hashmap.remove (Form.getid (). toString ()))
  24. {
  25. System.out.println ("Remove");
  26. }
  27. }
  28. HttpSession session=arg0.getsession ();
  29. Hashmap<string,managerform>allmanagerbysessionid= (hashmap<string,managerform>)  Application.getattribute ("Allmanagerbysessionid");
  30. if (Allmanagerbysessionid.containskey (Session.getid ()))
  31. {
  32. Allmanagerbysessionid.remove (Session.getid ());
  33. Application.setattribute ("Allmanagerbysessionid", Allmanagerbysessionid);
  34. }
  35. }


(2) Login Check page code

[Java]View PlainCopy
  1. Save Sesionid to Browser cookie
  2. Cookie cookie=New Cookie ("user", Request.getsession (). GetId ());
  3. Cookie.setmaxage (3600);
  4. Response.addcookie (cookie);
  5. //Save Form object to a specific HashMap
  6. Hashmap<string,managerform>allmanagerbysessionid= (hashmap<string,managerform>)  Appliction.getattribute ("Allmanagerbysessionid");
  7. Allmanagerbysessionid.put (Request.getsession (). GetId (), userForm);


(3) Browser login page check code

[Java]View PlainCopy
    1. <%
    2. cookie[] allcookies = Request.getcookies ();
    3. String Savedsessionid = "";
    4. if (allcookies! = null)
    5. {
    6. For (int i = 0; i < allcookies.length; i++)
    7. {
    8. if (Allcookies[i].getname (). Equals ("user"))
    9. {
    10. Savedsessionid = Allcookies[i].getvalue ();
    11. Break ;
    12. }
    13. }
    14. hashmap<string, managerform> Allmanagerbysessionid = (hashmap<string, managerform>) application
    15. . getattribute ("Allmanagerbysessionid");
    16. if (Allmanagerbysessionid.containskey (Savedsessionid))
    17. {
    18. Managerform form = Allmanagerbysessionid
    19. . get (Savedsessionid);
    20. Session.setattribute ("user", form);
    21. Response.sendredirect (Request.getcontextpath () +"/background/managerindex.jsp");
    22. return;
    23. }
    24. }
    25. %>

Using Session+application+cookie to realize single-state login and solve the problem of unexpected browser shutdown

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.