|
//清除Cookies
function clearCookie()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) {
WriteCookie("username",'');
WriteCookie("password",'');
document.getElementById("cookie_info").innerHTML = "清除成功";
}
//写cookie
function WriteCookie (cookieName, cookieValue, expiry)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) {
var expDate = new Date();
if(expiry) //如果设置了cookie失效时间;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
expDate.setTime (expDate.getTime() + expiry);
document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString();
}
else //没有设置cookie失效时间;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
document.cookie = cookieName + "=" + escape (cookieValue);
}
}
//写cookie
function setCookie()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var num = document.getElementById("cookieDate").value;
if(num == 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
WriteCookie("username", username, 0);
WriteCookie("password", password, 0);
}
else if(num == 1)
//如果选择的是天;时间换算成秒;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
WriteCookie("username", username, 1000 * 60 * 60 * 24 * num);
WriteCookie("password", password, 1000 * 60 * 60 * 24 * num);
}
else if(num == 2)
//如果选择的是月;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
WriteCookie("username", username, 1000 * 60 * 60 * 24 * num * 31);
WriteCookie("password", password, 1000 * 60 * 60 * 24 * num * 31);
}
else if(num == 3)
//如果选择的是年;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
WriteCookie("username", username, 1000 * 60 * 60 * 24 * num * 365);
WriteCookie("password", password, 1000 * 60 * 60 * 24 * num * 365);
}
}
//读cookie的值
function ReadCookie (CookieName)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) {
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++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CookiePieces = CookieSet[x].split ('=');
if (CookiePieces[0].substring (0,1) == ' ')
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
}
if (CookiePieces[0] == CookieName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
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%>
![](/Images/OutliningIndicators/None.gif)
<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>
|