怎么在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;
}
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询