<%
request.setCharacterEncoding("utf-8");
//首先判断用户是否选择了记住登录状态
String[] isUserCookies = request.getParameterValues("isUserCookie");
if(isUserCookies != null && isUserCookies.length>0){
//把用户名和密码保存在Cookie对象里面并且进行URL编码
String username = URLEncoder.encode(request.getParameter("username"),"utf-8");
//使用URLEncoder解决无法在Cookie当中保存中文字符串的问题
String password = URLEncoder.encode(request.getParameter("password"),"utf-8");
Cookie usernameCookie = new Cookie("username",username);
Cookie passwordCookie = new Cookie("password",password);
//设置最大生存期限为10天
usernameCookie.setMaxAge(864000);
passwordCookie.setMaxAge(864000);
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
}else{
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length>0){
for(Cookie c : cookies){
if(c.getName().equals("username") || c.getName().equals("password")){
c.setMaxAge(0); //设置Cookie失效
response.addCookie(c); //重新保存
}
}
}
}
%>
<%
request.setCharacterEncoding("utf-8");
String username = "";
String password = "";
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length>0){
for(Cookie c : cookies){
if(c.getName().equals("username")){
//使用URLDecoder进行解码
username = URLDecoder.decode(c.getValue(),"utf-8");
}
if(c.getName().equals("password")){
password = URLDecoder.decode(c.getValue(),"utf-8");
}
}
}
%>