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
- ServletContext Application;
- @Override
- public void sessioncreated (httpsessionevent arg0)
- {
- ServletContext application=arg0.getsession (). Getservletcontext ();
- Hashmap<string,managerform>allmanagerbysessionid= (hashmap<string,managerform>) Application.getattribute ("Allmanagerbysessionid");
- if (null==allmanagerbysessionid)
- {
- allmanagerbysessionid=new hashmap<string,managerform> ();
- Application.setattribute ("Allmanagerbysessionid", Allmanagerbysessionid);
- }
- System.out.println ("create");
- }
- @SuppressWarnings ("unchecked")
- @Override
- public void sessiondestroyed (httpsessionevent arg0)
- {
- Managerform form = (managerform) arg0.getsession (). getattribute ("user");
- Application=arg0.getsession (). Getservletcontext ();
- Hashmap<string, managerform>hashmap= (hashmap<string, managerform>) Application.getattribute (" Allmanager ");
- if (hashmap!=null)
- {
- if (null!= hashmap.remove (Form.getid (). toString ()))
- {
- System.out.println ("Remove");
- }
- }
- HttpSession session=arg0.getsession ();
- Hashmap<string,managerform>allmanagerbysessionid= (hashmap<string,managerform>) Application.getattribute ("Allmanagerbysessionid");
- if (Allmanagerbysessionid.containskey (Session.getid ()))
- {
- Allmanagerbysessionid.remove (Session.getid ());
- Application.setattribute ("Allmanagerbysessionid", Allmanagerbysessionid);
- }
- }
(2) Login Check page code
[Java]View PlainCopy
- Save Sesionid to Browser cookie
- Cookie cookie=New Cookie ("user", Request.getsession (). GetId ());
- Cookie.setmaxage (3600);
- Response.addcookie (cookie);
- //Save Form object to a specific HashMap
- Hashmap<string,managerform>allmanagerbysessionid= (hashmap<string,managerform>) Appliction.getattribute ("Allmanagerbysessionid");
- Allmanagerbysessionid.put (Request.getsession (). GetId (), userForm);
(3) Browser login page check code
[Java]View PlainCopy
- <%
- cookie[] allcookies = Request.getcookies ();
- String Savedsessionid = "";
- if (allcookies! = null)
- {
- For (int i = 0; i < allcookies.length; i++)
- {
- if (Allcookies[i].getname (). Equals ("user"))
- {
- Savedsessionid = Allcookies[i].getvalue ();
- Break ;
- }
- }
- hashmap<string, managerform> Allmanagerbysessionid = (hashmap<string, managerform>) application
- . getattribute ("Allmanagerbysessionid");
- if (Allmanagerbysessionid.containskey (Savedsessionid))
- {
- Managerform form = Allmanagerbysessionid
- . get (Savedsessionid);
- Session.setattribute ("user", form);
- Response.sendredirect (Request.getcontextpath () +"/background/managerindex.jsp");
- return;
- }
- }
- %>
Using Session+application+cookie to realize single-state login and solve the problem of unexpected browser shutdown