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; import javax.servlet.http.HttpSession; @RestController public class CookieSessionController { @RequestMapping(value = "/cookieTest1") //获取请求行数据 public void test1(HttpServletRequest request,HttpServletResponse response) { Cookie as = new Cookie("msg","hello"); as.setMaxAge(20); 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()); } } } @RequestMapping(value = "/sessionTest1") public void test3(HttpServletRequest request,HttpServletResponse response) { HttpSession session = request.getSession(); session.setAttribute("msg","session-hello"); } @RequestMapping(value = "/sessionTest2") public void test4(HttpServletRequest request,HttpServletResponse response) { HttpSession session = request.getSession(); Object as = session.getAttribute("msg"); System.out.println(as); } }
标签:spring,boot,request,HttpServletResponse,session,Cookie,import,response From: https://www.cnblogs.com/xiaobaibailongma/p/17048145.html