JAVA EE之Cookie记住我

1:form

2: servlet

//接收form传来的记住我
String remember=req.getParameter("ok");
//创建一个cookie,存放制定值
Cookie nameCookie = new Cookie("username", username);
Cookie passwordCookie = new Cookie("password", password);
//设置失效时间
if (null!=remember && "ok".equals(remember)){
    nameCookie.setMaxAge(7*24*60*60);
    passwordCookie.setMaxAge(7*24*60*60);
}else {
    nameCookie.setMaxAge(0);
    passwordCookie.setMaxAge(0);
}
//将Cookie存放到response中
resp.addCookie(nameCookie);
resp.addCookie(passwordCookie);

3:jsp

    <%
        String username = "";
        String password = "";
        Cookie [] cookies = request.getCookies();
        for (int i = 0; i 

 

设置form中的value="<%=username%"   value="<%=password%>"

 JAVA EE之Cookie记住我_第1张图片

你可能感兴趣的:(JAVA,EE)