怎么将数据存入cookie和取cookie
1.存入
//存cookie
@RequestMapping("/selectUsers7")
public String selectUsers7(HttpServletResponse response)
{
Cookie cookie=new Cookie("loginUname","zhangsan");
cookie.setPath("/");
cookie.setMaxAge(3600);
response.addCookie(cookie);
return "showUsers";//转发
}
2.取出
方式1:
//取cookie
@RequestMapping("/selectUsers8")
public String selectUsers8(HttpServletRequest request)
{
Cookie[] arr= request.getCookies();
if(arr!=null)
{
for(Cookie cookie:arr)
{
if(cookie.getName().equals("loginUname"))
System.out.println(cookie.getValue());
}
}
return "showUsers";
}
方式2:
@RequestMapping("/selectUsers9")
public String selectUsers9(@CookieValue(value="loginUname") String loginUname) {
System.out.println(loginUname);
return "showUsers";
}