} } function getCookieVal(offset) { var iEndStr=document.cookie.indexOf(";",offset); if(iEndStr==-1) iEndStr=document.cookie.length; return unescape(document.cookie.substring(offset,iEndStr)); }function getCookie(name) { var strArg=name+"="; var iArgLength=strArg.length; var iCookieLength=document.cookie.length; var iIndex=0; while(iIndex<iCookieLength) { var kIndex=iIndex+iArgLength; if(document.cookie.substring(iIndex,kIndex)==strArg) return getCookieVal(kIndex);
iIndex=document.cookie.indexOf(" ",iIndex)+1;
if(iIndex==0) break; } return null; }function setCookie(name,value) { var strArgValue=setCookie.arguments; var iArgLength=setCookie.arguments.length; var expires=(2<iArgLength)?strArgValue[2]:null; var path=(3<iArgLength)?strArgValue[3]:null; var domain=(4<iArgLength)?strArgValue[4]:null; var secure=(5<iArgLength)?strArgValue[5]:false; document.cookie=name+"="+escape(value)+((expires==null)?"":(";expires="+expires.toGMTString()))+ ((path==null)?"":(";path="+path))+((domain==null)?"":(";domain="+domain))+ ((secure==true)?";secure":""); }
login.java中
...
Cookie usercookie=new Cookie("usercookie",user.getUsername());
usercookie.setMaxAge(24*60*60);
usercookie.setPath("/koubeiTest/");
usercookie.setDomain("www.lifeng.com");
ServletActionContext.getResponse().addCookie(usercookie);
... login.jsp中
<%
Cookie[] cookie = request.getCookies();
if(cookie!=null){
for(int i = 0; i < cookie.length; i++){
Cookie myCookie = cookie[i];
if(myCookie.getName().equals("usercookie")){
response.sendRedirect("跳转到某用户的界面");
}}}%> 我的疑问是
function ckForm(){
var sname =document.loginForm.Login.value;
var snpwd =document.loginForm.Pwd.value;
var snexa =document.loginForm.examine.value; if(sname==""){
sname=null;
}
if(snpwd==""){
snpwd=null;
}
if(snexa==""){
snexa=null;
}
if(sname==null){
alert('please input name');
return false;
}else if(snpwd==null){
alert('please input pwd');
return false;
}else if(snexa==null){
alert('please input examine')
return false;
}
else{
}
}
function getCookieVal(offset)
{
var iEndStr=document.cookie.indexOf(";",offset);
if(iEndStr==-1) iEndStr=document.cookie.length;
return unescape(document.cookie.substring(offset,iEndStr));
}function getCookie(name)
{
var strArg=name+"=";
var iArgLength=strArg.length;
var iCookieLength=document.cookie.length;
var iIndex=0;
while(iIndex<iCookieLength)
{
var kIndex=iIndex+iArgLength;
if(document.cookie.substring(iIndex,kIndex)==strArg)
return getCookieVal(kIndex);
iIndex=document.cookie.indexOf(" ",iIndex)+1;
if(iIndex==0)
break;
}
return null;
}function setCookie(name,value)
{
var strArgValue=setCookie.arguments;
var iArgLength=setCookie.arguments.length;
var expires=(2<iArgLength)?strArgValue[2]:null;
var path=(3<iArgLength)?strArgValue[3]:null;
var domain=(4<iArgLength)?strArgValue[4]:null;
var secure=(5<iArgLength)?strArgValue[5]:false;
document.cookie=name+"="+escape(value)+((expires==null)?"":(";expires="+expires.toGMTString()))+
((path==null)?"":(";path="+path))+((domain==null)?"":(";domain="+domain))+
((secure==true)?";secure":"");
}
<tr>
<td align=left valign=middle width="40%">
username
</td>
<td>
<input size="14" type="text" name="Login" value="" class=textbox maxlength=20 tabindex=1>
</td>
</tr>
<tr>
<td align=left valign=middle >
password
</td>
<td>
<input size="14" name='Pwd' id="cookieValue" type='password' value="" class=textbox maxlength=20 onkeyup="value=value.replace(/[\W]/g,'')"
tabindex=2>
</td>
</tr>
<tr>
<td align=left valign=middle >
examine
</td>
<td>
<input size="4" type="text" name='examine' value="" class=textbox maxlength=20 onKeyUp="value=value.replace(/[^\d|]/g,'')" tabindex=3>
<img border=0 src="inc/image.jsp" width="56">
</td>
</tr>
</tavle>
打上“自动登陆" 写入Cookie
下次打开网页,先读出Cookie
用读出的Cookie验证