标签:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //防止中文乱码 response.setContentType("text/html;charset=UTF-8"); PrintWriter out=response.getWriter(); out.write(" 你上次访问的时间是:"); //获取本地的cookie Cookie[] cookies=request.getCookies(); for(int i=0;cookies!=null && i<cookies.length;i++){ if(("lastAccessTime").equals(cookies[i].getName())){ //字符串的日期转换为long,在通过Date转化为日期,通过toLocalString 来输出 Long cookieValue=Long.parseLong(cookies[i].getValue()); Date date=new Date(cookieValue); out.write(" 你上次访问的时间是:"); out.write(date.toLocaleString()); } } //把cookie写回本地去 Cookie cookie=new Cookie("lastAccessTime",System.currentTimeMillis()+""); cookie.setMaxAge(1*24*3600); cookie.setPath("/day08"); response.addCookie(cookie); }
标签:
原文地址:http://www.cnblogs.com/lyjs/p/4890335.html