【Servlet】Cookie应用:显示上次访问页面时间

#Q题目

要求:

显示上次访问页面时间,并添加清除Cookie功能

如下图:

这里写图片描述

这里写图片描述

清除Cookie

这里写图片描述


#A 代码

实现Cookie显示上次访问时间

package com.tcb.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *说明:
 *1.该方式将时间直接以long类型存进Cookie
 *2.从Cookie中取出值后,需要先做日期格式化,才能展示
 *
 *优点:避免某些电脑浏览器因为Cookie值存储长短限制不能显示上次访问时间
 *
 *比如:“2017-05-04 20:33:22”,日期转为该格式后,不能存进Cookie中,因为Cookie长度限制
 *	 但直接以--时间戳(long类型)--的方式,可以直接存进Cookie
 */
public class CookieServlet01 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CookieServlet01() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//编码格式
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer=response.getWriter();
		
		writer.write("上次访问时间:");
		
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
			//遍历cookies,取出lastAccessTime这个cookie
			for (int i = 0; i < cookies.length; i++) {
				Cookie c=cookies[i];
				if("lastAccessTime".equals(c.getName())){
					//取值上次访问时间,并显示出来
					String time=c.getValue();
					time=new Date(Long.parseLong(time)).toLocaleString();
					writer.write(String.valueOf(time));
				}
			}
		}
		
		writer.write("<a href="+request.getContextPath()+"/ClearCookieServlet>清除Cookie</a>");
		
		//获取当前时间,并存储到Cookie中
		Long currentTime=System.currentTimeMillis();
		Cookie cookie=new Cookie("lastAccessTime", currentTime.toString());
		cookie.setMaxAge(5*60);//设置Cookie有效时间,5min内关闭再打开浏览器Cookie会一直存在
		response.addCookie(cookie);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

清除Cookie

package com.tcb.cookie;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 说明:
 *      清楚Cookie
 */
public class ClearCookieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ClearCookieServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//清除cookie
		
		//清除cookie时,完全没必要先将cookie遍历出来,再去清除cookie,尽管下面的方法可以实现清除cookie
		//我们可以根据cookie的特性,名字相同的cookie,后面设置的值会覆盖前面的值
		/*Cookie[] cookies=request.getCookies();
		if(cookies!=null){
			for (Cookie cookie : cookies) {
				if("lastAccessTime".equals(cookie.getName())){
					cookie.setMaxAge(0);
					response.addCookie(cookie);
				}
			}
		}*/
		
		
		//便捷做法
		Cookie cookie=new Cookie("lastAccessTime", "");
		cookie.setMaxAge(0);
		response.addCookie(cookie);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值