Cookie
十天免登陆
思路:
1.login.jsp中request 携带的Cookie 找出 我们需要的 value;
2.在 form 中把步骤一的value 赋给对应的 name
3.创建新 Cookie
4.response覆盖添加
注意:
1.login界面的赋值
2.新增Cookie之后不要忘记 在response中添加
login.jsp代码:
<body>
<%
String username_login="";
String password_login="";
Cookie[] co=request.getCookies();
if(co!=null){
for(Cookie c:co){
if(c.getName().equals("username")){
username_login=c.getValue();
}
if(c.getName().equals("password")){
password_login=c.getValue();
}
}
}
%>
<form action="doLogin.jsp">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="<%= username_login%>"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value="<%= password_login%>"></td>
</tr>
<tr>
<td colspan="2"> <input type="checkbox" checked="checked" name="X" value="Y">十天内免登陆</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
doLogin.jsp 代码:
<%
Cookie[] c=request.getCookies();
if(request.getParameter("X")!=null){
Cookie a=new Cookie("username",request.getParameter("username"));
Cookie b=new Cookie("password",request.getParameter("password"));
a.setMaxAge(600000);
b.setMaxAge(600000);
response.addCookie(a);
response.addCookie(b);
}
else{
for(Cookie co:c){
if(co.getName().equals("username")){
co.setMaxAge(0);
response.addCookie(co);}
if(co.getName().equals("password")){
co.setMaxAge(0);
response.addCookie(co);}
}
}
%>