• javaWeb 使用cookie显示上次访问网站时间


    package de.bvb.cookie;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Date;
    
    import javax.enterprise.inject.ResolutionException;
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.swing.text.DateFormatter;
    
    /**
     * 显示上次访问时间
     * 
     * @author joker
     * 
     */
    public class CookieDemo1 extends HttpServlet {
    
        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.write("您上次访问的时间是: ");
    
            // 获取上次访问时间
            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());
                    out.write(new Date(cookieValue).toLocaleString());
                }
            }
    
            // 保存本次访问时间
            Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()
                    + "");
            response.addCookie(cookie);
    
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            doGet(request, response);
        }
    
    }
  • 相关阅读:
    解决Java版CKFinder无法显示缩略图问题
    python视频教程大全
    关于Linux vi命令 vi命令一览表
    Python快速教程
    Linux的概念与体系
    每天一个linux命令目录
    每天一个linux命令(31): /etc/group文件详解
    每天一个linux命令(30): chown命令
    每天一个linux命令(29):chgrp命令
    十大Material Design开源项目
  • 原文地址:https://www.cnblogs.com/Westfalen/p/5962876.html
Copyright © 2020-2023  润新知