Cookie

作者: 宋song一 | 来源:发表于2018-09-07 21:09 被阅读6次

查看上次访问时间

依赖
compile("org.springframework.boot:spring-boot-starter-web:1.5.15.RELEASE")
compile("org.thymeleaf:thymeleaf-spring4:3.0.9.RELEASE")

入口函数
package com.bat;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}



package com.bat.talk;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
public class lastcontrol {

    String result;

    @RequestMapping("/home")
    public String home(HttpServletRequest request, HttpServletResponse response) {
        ;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("lasttime".equals(cookie.getName())) {
                    String value = cookie.getValue();
                    Date date = new Date(Long.parseLong(value));

                    result = "您上次的访问时间是:" + date.toLocaleString();

                } else {
                    result = "您是第一次访问,欢迎您";
                }
            }
        }

        Cookie cookie = new Cookie("lasttime", System.currentTimeMillis() + "");
        cookie.setMaxAge(60 * 60);
        response.addCookie(cookie);
        return result;
    }
}

相关文章

网友评论

      本文标题:Cookie

      本文链接:https://www.haomeiwen.com/subject/gpjhgftx.html