• java_Cookie_example(你上次访问的时间)


    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		response.setCharacterEncoding("UTF-8");
    		response.setContentType("text/html;charset=UTF-8");
    
    		PrintWriter out = response.getWriter();
    		out.print("你上次访问时间是: ");
    
    		// 获得用户的时间cookie
    		Cookie[] cookies = request.getCookies();
    		for (int i = 0; cookies != null && i < cookies.length; i++) {
    			if (cookies[i].getName().equals("lastAccessTime")) {
    				long cookieValue = Long.parseLong(cookies[i].getValue());// 用户上次访问时间
    				Date date = new Date(cookieValue);
    				out.print(date.toLocaleString());
    			}
    		}
    		
    		//给用户回送最新的访问时间
    		Cookie cookie = new Cookie("lastAccessTime",System.currentTimeMillis()+"");
    		cookie.setMaxAge(1*30*24*3600);
    		cookie.setPath("/Test");
    		
    		response.addCookie(cookie);
    		
    	}


  • 相关阅读:
    图片懒加载
    浅谈javascript的函数节流
    js字符串常用方法详解
    js数组详解
    thinkphp3.2 批量添加数据
    openssl证书及配置
    手机访问PC端
    mui框架(三)
    mui框架(二)
    mui框架(一)
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720433.html
Copyright © 2020-2023  润新知