• Cookie登录时间案例 January 27,2020


    记住上一次访问时间
    1. 需求:
      1. 访问一个Servlet,如果是第一次访问,则提示:您好,欢迎您首次访问。
      2. 如果不是第一次访问,则提示:欢迎回来,您上次访问时间为:显示时间字符串

    2. 分析:
      1. 可以采用Cookie来完成
      2. 在服务器中的Servlet判断是否有一个名为lastTime的cookie
        1. 有:不是第一次访问
          1. 响应数据:欢迎回来,您上次访问时间为:2018年6月10日11:50:20
          2. 写回Cookie:lastTime=2018年6月10日11:50:01
        2. 没有:是第一次访问
          1. 响应数据:您好,欢迎您首次访问
        2. 写回Cookie:lastTime=2018年6月10日11:50:01

    3. 代码实现:

    package cookie;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.net.URLDecoder;
    import java.net.URLEncoder;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    /**
     在服务器中的Servlet判断是否有一个名为lastTime的cookie
     1. 有:不是第一次访问
         1. 响应数据:欢迎回来,您上次访问时间为
         2. 写回Cookie
     2. 没有:是第一次访问
         1. 响应数据:您好,欢迎您首次访问
         2. 写回Cookie:
     */
    @WebServlet("/cookieTest")
    public class CookieTest extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html;charset=utf-8");
            //1.获取所有Cookies
            Cookie[] cookies = request.getCookies();
            boolean flag = false;//没有cookie为lastTime
            //2.遍历所有的Cookie
            if(cookies != null && cookies.length !=0){
                for (Cookie cookie : cookies) {
                    //3.获取所有的Cookie
                    String cookieName = cookie.getName();
                    //4.判断是否是第一次登陆
                    if("lastTime".equals(cookieName)){
                        //不是第一次登陆
                        flag = true;
                        //获取上次登陆的时间值
                        String date_format = cookie.getValue();
                        //响应数据
                        System.out.println("解码前:"+date_format);
                        //URL解码:
                        date_format = URLDecoder.decode(date_format,"utf-8");
                        System.out.println("解码后:"+date_format);
                        response.getWriter().write("欢迎回来,您上次访问时间为:"+date_format);
    
                        //时间
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                        String timeValue = sdf.format(date);//将本次登陆的时间转换
                        System.out.println("当前时间为:"+timeValue);
    
                        System.out.println("编码前:"+timeValue);
                        //URL编码
                        timeValue = URLEncoder.encode(timeValue,"utf-8");
                        System.out.println("编码后:"+timeValue);
                        //设置cookie的存活时间
                        cookie.setMaxAge(60 * 60 * 24 * 30);//一个月
                        cookie.setValue(timeValue);
                        response.addCookie(cookie);
                        break;
                    }
                }
            }
            if(cookies == null || cookies.length == 0 || flag == false){
                //没有,第一次访问
                //设置Cookie的value
                //获取当前时间的字符串,重新设置Cookie的值,重新发送cookie
                Date date  = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                String date_format = sdf.format(date);
                System.out.println("编码前:"+date_format);
                //URL编码
                date_format = URLEncoder.encode(date_format,"utf-8");
                System.out.println("编码后:"+date_format);
                Cookie cookie = new Cookie("lastTime",date_format);
                //设置cookie的存活时间
                cookie.setMaxAge(60 * 60 * 24 * 30);//一个月
                response.addCookie(cookie);
                response.getWriter().write("<h1>您好,欢迎您首次访问</h1>");
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }
  • 相关阅读:
    MySQL创建数据库与创建用户以及授权
    java关于map用来筛选的用法
    C#实体类的关联运用
    PHP 数据库基础操作
    PHP 文件操作
    PHP 加密和解密
    PHP 图形处理
    PHP Cookie和Session
    SQL基本操作
    JAVA基本术语
  • 原文地址:https://www.cnblogs.com/yyanghang/p/12236108.html
Copyright © 2020-2023  润新知