如何在浏览器上永久保存某些值?

问题描述 投票:0回答:2

我有一些登录信息;假设用户名、登录电子邮件和位置。

即使用户注销并关闭窗口后,我也希望将此信息保留在浏览器中。

当用户注销或会话过期后返回时,Web 应用程序会填写客户端用户名并要求用户提供密码。我的要求最好的例子是 Google 登录。

目前,我仅使用会话,没有使用 cookie。

有哪些可能的解决方案?

java session jsf-2 primefaces
2个回答
2
投票

我认为您可以使用 cookie 在客户端存储数据,请点击此链接

http://www.tutorialspoint.com/jsp/jsp_cookies_handling.htm

使用方法设置存储年龄 public void setMaxAge(int expiry);

另一种解决方案是HTML5中的本地存储 但这仅在最新的浏览器中受支持。

http://www.w3schools.com/html/html5_webstorage.asp

http://diveintohtml5.info/storage.html

希望这些链接对您有帮助


0
投票

LocalStorage被认为是在浏览器中永久存储值的最佳解决方案。!! 关于 LocalStorage 的详细解释可以在 here 找到。

这是我用于将值保存到LocalStorage的代码。

         function saveLoginNameToLocalStorage()  
                {
                 if(typeof(Storage)!=="undefined")//checks whether the browser support localStorage
                  {
                        // you dont want to create a variable by var variablename, 
                        // just give it as localStorage.yourVariableName, before assigning 
                        // any values the variable is shown as  undefined.
                         if(localStorage.userName && localStorage.userName !="" && localStorage.userName==document.getElementById("userName").value){
                            document.getElementById("redirectUrl").value=localStorage.redirectURI;
                        }
                        else{
                            localStorage.redirectURI="";
                            document.getElementById("redirectUrl").value="";
                        }
                         localStorage.userName=document.getElementById("userName").value;
                         localStorage.redirectURI="";
                  } 
                }

您可以从浏览器中的任何位置使用

localStorage.userName
访问变量。对我来说效果很好。 ;-)

感谢大家提供的帮助..!!

© www.soinside.com 2019 - 2024. All rights reserved.