使用Cookie实现显示用户上次访问时间

一. 经常使用Cookie API介绍

1. 获取cookiehtml

request.getCookies();  // 返回Cookie[]web

2. 建立cookie浏览器

Cookie(String key, String value);  // 经过构造器建立缓存

3. 返回cookie给用户浏览器cookie

response.addCookie(Cookie cookie);dom

4. 设置cookie的有效期jsp

cookie.setMaxAge(int sec);ide

1) 单位: 秒测试

2) 默认cookie有效期为一个会话, 存储在浏览器缓存中, 即关闭浏览器cookie被删除this

3) 设置了有效期的cookie, 即便关闭了浏览器, 也不会被删除, 即存储在硬盘上, 一般是在浏览器缓存目录下

4) 将cookie的有效期设置为0时, 表示删除该cookie, 值得注意的是删除cookie时的有效路径应该与建立cookie时的有效路径一致, 不然没法删除

5. 设置cookie的有效路径

cookie.setPath(String path);

1) cookie的默认有效路径: 建立该cookie的servlet所在的servlet映射路径. 好比在CookieDemo1建立了cookie1, 并且知道CookieDemo1的映射路径是/day01/servlet/CookieDemo1 (/day01为Web工程根路径) , 那么cookie1的有效路径就是/day01/servlet/CookieDemo1

2) 删除cookie时设置的有效路径与建立cookie时设置的有效路径一致, 才能删除cookie

3) 将cookie的有效目录设置成/day01时, 则访问/day01目录下全部资源都会携带cookie. 若是将cookie的有效目录设置成/day01/resource/, 那么在访问/day01/index.jsp时就不会携带cookie过去.

6. 设置cookie域

cookie.setDomain(string domain);

1) 什么是域?

sina.com.cn是域名

www.sina.com.cn是主机名, 表示sina.com.cn域下有一台www主机

同理也能够是ftp, mail主机, ftp.sina.com.cn, 就相似咱们购买好域名以后能够在域上搭建多台主机

2) 举个栗子

cookie.setDomain(".sina.com.cn");  // 之后去访问sina.com.cn域时, 都会携带这个cookie过去, 注意sina.com.cn前有个点

7. support getter/setter for name and value

二. 实现显示用户上次访问时间

程序主要分红两个部分: 获取上次访问时间的cookie部分和返回最新访问时间的cookie, 值得注意的是这两部分使用的是同一个cookie 

 1 /**
 2  * Created by IntelliJ IDEA.  3  *  4  * @Auther: ShaoHsiung  5  * @Date: 2018/8/28 08:22  6  * @Title: 获取用户上次访问时间并返回最新的访问时间  7  * @Description:  8  */
 9 public class CookieDemo1 extends HttpServlet { 10  @Override 11     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 12 
13         // 设置浏览器编码
14         response.setContentType("text/html; charset=utf-8"); 15 
16         // 获取输出对象
17         Writer out = response.getWriter(); 18         out.write("上次访问时间: "); 19 
20         // 获取访问时间cookie
21         Cookie[] cookies = request.getCookies(); 22         for (int i = 0; cookies != null && i < cookies.length; i++) { 23 
24             Cookie cookie = cookies[i]; 25             if (cookie.getName().equals("lastAccess")) { 26                 String value = cookie.getValue(); 27                 Date date = new Date(Long.parseLong(value)); 28 
29  out.write(date.toLocaleString()); 30  } 31  } 32 
33         // 设置最新的访问时间cookie
34         Cookie cookie = new Cookie("lastAccess", System.currentTimeMillis() + ""); 35         // 设置cookie有效时间 单位:秒
36         cookie.setMaxAge(3600); 37         // 设置cookie有效路径
38  cookie.setPath(request.getContextPath()); 39         //System.out.println(request.getContextPath()); 40         //System.out.println(this.getServletContext().getContextPath());
41  response.addCookie(cookie); 42  } 43 
44  @Override 45     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 46  doGet(req, resp); 47  } 48 }

 三. cookie其它细节

1. 一个web站点能够给用户发送多个cookie

2. 不一样浏览器cookie的个数和大小不一致

3. 每一个web站点最多给用户发送20个cookie  // 未测试

相关文章
相关标签/搜索