|
//清除Cookies
function clearCookie()
 {
WriteCookie("username",'');
WriteCookie("password",'');
document.getElementById("cookie_info").innerHTML = "清除成功";
}
//寫cookie
function WriteCookie (cookieName, cookieValue, expiry)
 {
var expDate = new Date();
if(expiry) //如果設置了cookie失效時間;
 {
expDate.setTime (expDate.getTime() + expiry);
document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString();
}
else //沒有設置cookie失效時間;
 {
document.cookie = cookieName + "=" + escape (cookieValue);
}
}
//寫cookie
function setCookie()
 {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var num = document.getElementById("cookieDate").value;
if(num == 0)
 {
WriteCookie("username", username, 0);
WriteCookie("password", password, 0);
}
else if(num == 1)
//如果選擇的是天;時間換算成秒;
 {
WriteCookie("username", username, 1000 * 60 * 60 * 24 * num);
WriteCookie("password", password, 1000 * 60 * 60 * 24 * num);
}
else if(num == 2)
//如果選擇的是月;
 {
WriteCookie("username", username, 1000 * 60 * 60 * 24 * num * 31);
WriteCookie("password", password, 1000 * 60 * 60 * 24 * num * 31);
}
else if(num == 3)
//如果選擇的是年;
 {
WriteCookie("username", username, 1000 * 60 * 60 * 24 * num * 365);
WriteCookie("password", password, 1000 * 60 * 60 * 24 * num * 365);
}
}
//讀cookie的值
function ReadCookie (CookieName)
 {
var CookieString = document.cookie;
var CookieSet = CookieString.split (';');
var SetSize = CookieSet.length;
var CookiePieces
var ReturnValue = "";
var x = 0;
for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++)
 {
CookiePieces = CookieSet[x].split ('=');
if (CookiePieces[0].substring (0,1) == ' ')
 {
CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
}
if (CookiePieces[0] == CookieName)
 {
ReturnValue = CookiePieces[1];
}
}
alert ("Cookie Value is:"+ReturnValue);
}
form:
<form action="${ctx}/user.do?method=login" method="post" onsubmit="return check();">
<table cellpadding=0 cellspacing=0 border=0 width=98%
bgcolor=#0099cc align=center>
<tr>
<td>
<table cellpadding=6 cellspacing=1 border=0 width=100%>

<tr>
<td bgcolor=#99ccff valign=middle colspan=2 align=center>
<!-- 判斷用戶名或密碼是否錯誤 -->
<logic:empty name="error" scope="request">
<font color="green"><b>請輸入您的用戶名、密碼登陸</b></font>
</logic:empty>
<logic:notEmpty name="error" scope="request">
<bean:message key="login.error"/>
</logic:notEmpty>
</td>
</tr>
<tr>
<td bgcolor=#f2f8ff valign=middle>
請輸入您的用戶名
</td>
<td bgcolor=#f2f8ff valign=middle>
<INPUT value="${requestScope.username}" name="username" type=text id="username" onblur="return check();">
<a href="${ctx}/main/common/reg.jsp">沒有注冊?</a>
</td>
<!-- 用戶名錯誤提示信息 -->
<td id="username_info" style="color:red;position: absolute;left:550px;top:235px;"></td>
</tr>
<tr>
<td bgcolor=#f2f8ff valign=middle>
請輸入您的密碼
</td>
<td bgcolor=#f2f8ff valign=middle>
<INPUT value="${requestScope.password}" name="password" type=password id="password" onblur="return check();">
<a href="lostpass.jsp">忘記密碼?</a>
</td>
<!-- 密碼錯誤提示信息 -->
<td id="password_info" style="color:red;position: absolute;left:550px;top:270px;"></td>
</tr>
<tr>
<td bgcolor=#f2f8ff valign=top width=30%>
<b>Cookie 選項</b>
<BR>
請選擇您的Cookie保存時間<br>
下次訪問可以方便輸入<br><br />
<a href="#" onclick="clearCookie();" style="color:green;">清空Cookies</a>
<div id="cookie_info" style="color:maroon;position: absolute;left:100px;top:360px;"></div>
</td>
<td bgcolor=#f2f8ff valign=middle>
<input type="radio" id="cookieDate" name="cookieDate" value="0" checked>
不保存,關閉瀏覽器就失效
<br>
<input type="radio" id="cookieDate" name="cookieDate" value="1">
保存一天
<br>
<input type="radio" id="cookieDate" name="cookieDate" value="2">
保存一月
<br>
<input type="radio" id="cookieDate" name="cookieDate" value="3">
保存一年
<br>
</td>
</tr>
<input type=hidden name=comeURL
value="#" />
<tr>
<td bgcolor=#99ccff valign=middle colspan=2 align=center>
<input type=submit name="submit" onclick="setCookie();" value="登 陸">
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
|