怎么在java中使用cookie

如题所述

public void addSessionCookie(String name, String val, HttpServletResponse response, String path) {
Cookie cookie = new Cookie(name, val);
cookie.setPath(path);
response.addCookie(cookie);
}

private void addCookie(String name, String val, HttpServletResponse response) {
Cookie cookie = new Cookie(name, val);
cookie.setMaxAge(COOKIE_MAXAGE);//失效时间
cookie.setPath("/");
response.addCookie(cookie);
}

/**
* 查找cookie(未区分版本)
* @param name
* @param req
* @return
*/
private Cookie findCookie(String name, HttpServletRequest req) {
if(req.getCookies()!=null) {
for (Cookie cookie : req.getCookies()) {
if(cookie.getName().equals(name)) {
return cookie;
}
}
}
return null;
}
温馨提示:答案为网友推荐,仅供参考
相似回答