Cookie記錄用戶名密碼
/////////////////////////////////////////////////////////////
jsp(js):-----------------------------------------------------
/////////////////////////////////////////////////////////////
<script>
function getCookieValue(tname){
var name = escape(tname);
//讀cookie屬性,這將返回文檔的所有cookie
var allcookies = document.cookie;
//alert("allcookies="+allcookies);
//查找名爲name的cookie的開始位置
name += "=";
var pos = allcookies.indexOf(name);
//如果找到了具有該名字的cookie,那麼提取並使用它的值
if (pos != -1){ //如果pos值爲-1則說明搜索"version="失敗
var start = pos + name.length; //cookie值開始的位置
var end = allcookies.indexOf(";",start); //從cookie值開始的位置起搜索第一個";"的位置,即cookie值結尾的位置
if (end == -1) end = allcookies.length; //如果end值爲-1說明cookie列表裏只有一個cookie
var value = allcookies.substring(start,end); //提取cookie的值
return unescape(value); //對它解碼
}
else return ""; //搜索失敗,返回空字符串
}
function initNameAndPassword()
{
//分析cookie值,顯示上次的登陸信息
var userNameValue = getCookieValue("username");
var passwordValue = getCookieValue("password");
var checkcookie = getCookieValue("checkcookie");
// $('#userName').value = "ddd";
document.getElementById("userName").value=userNameValue;
document.getElementById("password").value=passwordValue;
if(checkcookie!=null&&checkcookie=="true"){
document.getElementById("saveCookie").checked="true";}
//$('#password').value = "ddd";
}
</script>
//////////////////////////////////////////////////////////////////////
action(用戶名密碼驗證完成之後的代碼):-------------------------------------
//////////////////////////////////////////////////////////////////////
/**
* Cookie操作,保存Cookies和刪除cookie
*/
if(saveCookie!=null&&saveCookie.length!=0)
{
log.info("記錄Cookie開始~~~~~~~~~~~~~~~~");
Cookie namecookie = new Cookie("username",name);
Cookie passwordcookie = new Cookie("password",password);
Cookie checkcookie=new Cookie("checkcookie","true");
//生命週期 一年
namecookie.setMaxAge(60*60*24*365);
passwordcookie.setMaxAge(60*60*24*365);
checkcookie.setMaxAge(60*60*24*365);
getResponse().addCookie(namecookie);
getResponse().addCookie(passwordcookie);
getResponse().addCookie(checkcookie);
log.info("記錄Cookie結束~~~~~~~~~~~~~~~~");
}
else
{
log.info("刪除Cookie開始~~~~~~~~~~~~~~~~");
Cookie[] cookies=getRequest().getCookies();
//cookies不爲空,則清除
for(int i=0;i<cookies.length;i++)
{
String value=cookies[i].getName();
//查找用戶名
if(value.equals("username")||value.equals("password")||value.equals("checkcookie"))
{
cookies[i].setMaxAge(0);
getResponse().addCookie(cookies[i]);
}
}
log.info("刪除Cookie結束~~~~~~~~~~~~~~~~");
}
jsp(js):-----------------------------------------------------
/////////////////////////////////////////////////////////////
<script>
function getCookieValue(tname){
var name = escape(tname);
//讀cookie屬性,這將返回文檔的所有cookie
var allcookies = document.cookie;
//alert("allcookies="+allcookies);
//查找名爲name的cookie的開始位置
name += "=";
var pos = allcookies.indexOf(name);
//如果找到了具有該名字的cookie,那麼提取並使用它的值
if (pos != -1){ //如果pos值爲-1則說明搜索"version="失敗
var start = pos + name.length; //cookie值開始的位置
var end = allcookies.indexOf(";",start); //從cookie值開始的位置起搜索第一個";"的位置,即cookie值結尾的位置
if (end == -1) end = allcookies.length; //如果end值爲-1說明cookie列表裏只有一個cookie
var value = allcookies.substring(start,end); //提取cookie的值
return unescape(value); //對它解碼
}
else return ""; //搜索失敗,返回空字符串
}
function initNameAndPassword()
{
//分析cookie值,顯示上次的登陸信息
var userNameValue = getCookieValue("username");
var passwordValue = getCookieValue("password");
var checkcookie = getCookieValue("checkcookie");
// $('#userName').value = "ddd";
document.getElementById("userName").value=userNameValue;
document.getElementById("password").value=passwordValue;
if(checkcookie!=null&&checkcookie=="true"){
document.getElementById("saveCookie").checked="true";}
//$('#password').value = "ddd";
}
</script>
//////////////////////////////////////////////////////////////////////
action(用戶名密碼驗證完成之後的代碼):-------------------------------------
//////////////////////////////////////////////////////////////////////
/**
* Cookie操作,保存Cookies和刪除cookie
*/
if(saveCookie!=null&&saveCookie.length!=0)
{
log.info("記錄Cookie開始~~~~~~~~~~~~~~~~");
Cookie namecookie = new Cookie("username",name);
Cookie passwordcookie = new Cookie("password",password);
Cookie checkcookie=new Cookie("checkcookie","true");
//生命週期 一年
namecookie.setMaxAge(60*60*24*365);
passwordcookie.setMaxAge(60*60*24*365);
checkcookie.setMaxAge(60*60*24*365);
getResponse().addCookie(namecookie);
getResponse().addCookie(passwordcookie);
getResponse().addCookie(checkcookie);
log.info("記錄Cookie結束~~~~~~~~~~~~~~~~");
}
else
{
log.info("刪除Cookie開始~~~~~~~~~~~~~~~~");
Cookie[] cookies=getRequest().getCookies();
//cookies不爲空,則清除
for(int i=0;i<cookies.length;i++)
{
String value=cookies[i].getName();
//查找用戶名
if(value.equals("username")||value.equals("password")||value.equals("checkcookie"))
{
cookies[i].setMaxAge(0);
getResponse().addCookie(cookies[i]);
}
}
log.info("刪除Cookie結束~~~~~~~~~~~~~~~~");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.