四.Cookie的使用

119 阅读1分钟

1. 创建Cookie对象

Cookie cookie = new Cookie("uname","Tom");

2. 在客户端保存Cookie

//将这个Cookie对象保存到浏览器端
response.addCookie(cookie);
requset.getRequestDispatcher("index.html").forward(requset,response);

3. 设置Cookie的有效时长

  •   cookie.setMaxAge(60)  , 设置cookie的有效时长是60
  •   cookie.setDomain(pattern);
    
  •   cookie.setPath(uri);
    

4. Cookie的应用:

 4-1: 记住用户名和密码十天 setMaxAge(60 * 60 * 24 * 10)
 4-2: 十天免登录

5.Cookie与session的区别

[I%43ND{}WLS[}B)O`M4LQ8.png