首页 > 其他分享 >怎样将数据存入cookie和取出cookie

怎样将数据存入cookie和取出cookie

时间:2022-10-23 22:03:48浏览次数:52  
标签:arr loginUname String 存入 Cookie cookie 取出 public

怎么将数据存入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";
}

标签:arr,loginUname,String,存入,Cookie,cookie,取出,public
From: https://blog.51cto.com/u_15707781/5787610

相关文章