package org.example.controller; 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; @RestController public class CookieSessionController { @RequestMapping(value = "/cookieTest1") //获取请求行数据 public void test1(HttpServletRequest request,HttpServletResponse response) { Cookie as = new Cookie("msg","hello"); response.addCookie(as); response.addCookie(new Cookie("cook-001-name" ,"cookie-001-value")); } @RequestMapping(value = "/cookieTest2") public void test15(HttpServletRequest request,HttpServletResponse response) { Cookie[] cookies = request.getCookies(); if ( cookies != null) { for (Cookie c : cookies) { System.out.println(c.getName()+" "+c.getValue()); } } } }
先访问:http://localhost:8080/cookieTest1
再访问:http://localhost:8080/cookieTest2
标签:cookies,http,示例,spring,Cookie,import,cookie,response From: https://www.cnblogs.com/xiaobaibailongma/p/17045192.html