当前位置 : 主页 > 编程语言 > java >

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

来源:互联网 收集:自由互联 发布时间:2022-10-26
怎么将数据存入cookie和取cookie 1.存入 //存cookie @RequestMapping("/selectUsers7") public String selectUsers7(HttpServletResponse response) { Cookie cookie=new Cookie("loginUname","zhangsan"); cookie.setPath("/"); cookie.setMaxAg

怎么将数据存入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";
}
上一篇:Java多线程(3):ThreadPool(上)
下一篇:没有了
网友评论