在web网页中经常可以看到记住我这样的功能,其实现原理是登陆时候在response中写入cookie,发送请求时,取出cookie判断,如果有则说明已经登陆
写cookie
Cookie cookie = new Cookie("name", "123"); cookie.setMaxAge(24*60*60); response.addCookie(cookie);
读cookie
Cookie[] cookies = request.getCookies(); boolean flag = false; if(cookies != null) { for(Cookie c : cookies) { String name = c.getName(); if("name".equals(name)) { System.out.println("存在cookoe123"); flag = true; break; } } }