<%...@page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>jsp 利用cookie 記住使用者名稱和密碼</title>
</head>
<%...
if(request.getParameter("send")!=null)
{
Cookie cookie=new Cookie(request.getParameter("userno"), request.getParameter("password"));
cookie.setMaxAge(360*24*60*60);
response.addCookie(cookie);
}
Cookie cookies[]=request.getCookies();
Cookie sCookie=null;
String svalue=null;
String sname=null;
%>
<script language="javascript">...
var UserNo=new Array(); UserNo[0]="";
var Password=new Array(); Password[0]="";
</script>
<%...
for(int i=0;i<cookies.length;i++){
sCookie=cookies[i];
%>
<script language="javascript">...
UserNo[<%=i%>]="<%=sCookie.getName()%>";
Password[<%=i%>]="<%=sCookie.getValue()%>";
</script>
<%...
out.print(sCookie.getName()+" "+sCookie.getValue()+"</br>");
}
%>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<input type="text" name="userno" />
<input type="submit" name="send" value="s" />
</p>
<p>
<input type="text" name="password" onclick="return oUserNo_onchange()"/>
</p>
</form>
</body>
</html>
<SCRIPT language=javascript>...
function oUserNo_onchange()...{
for(i=0;i<UserNo.length;i++)
if(form1.userno.value==UserNo[i])
form1.password.value=Password[i];
}
</script>