用户登录成功后重新获取新的Session


          

 

 

 

 

HttpSession session = request.getSession();

            // 用来存储原sessionde的值
            ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();

            Enumeration enumeration = session.getAttributeNames();
            // 遍历enumeration
            while (enumeration.hasMoreElements()) {
                // 获取session的属性名称
                String name = enumeration.nextElement().toString();
                // 根据键值取session中的值
                concurrentHashMap.put(name,session.getAttribute(name));
            }

            // 获取之前旧的session
            HttpSession oldSession = request.getSession(false);
            if (oldSession != null) {
                //废除掉登陆前的session
                oldSession.invalidate();
            }
            request.getSession(true);
            // 获取新session
            session = request.getSession();
            // 将原先老session的值存入
            Iterator<Map.Entry<String, String>> it = concurrentHashMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> entry = it.next();
                session.setAttribute(entry.getKey(), entry.getValue());
            }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM