This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
CookieUtil
feilong edited this page Jun 3, 2016
·
15 revisions
Cookie 工具类.
注意:该类创建Cookie仅支持Servlet3
以上的版本
使用说明:
(通常出于安全起见,存放到Cookie的值需要加密或者混淆,此处为了举例方便使用原码)
CookieUtil.addCookie("shopName","feilong",response)
注意:该方法创建的cookie,为会话Cookie
,有效期是默认值 -1
,即浏览器退出就删除
如:
CookieUtil.addCookie("shopName","feilong", TimeInterval.SECONDS_PER_DAY,response)
CookieEntity cookieEntity = new CookieEntity("shopName", "feilong", TimeInterval.SECONDS_PER_DAY);
cookieEntity.setHttpOnly(true);
CookieUtil.addCookie(cookieEntity, response);
此外,如果有特殊需求,还可以对cookieEntity设置 path
,domain
等属性
CookieUtil.getCookie(request, "shopName")
CookieUtil.getCookieValue(request, "shopName")
返回 feilong
字符串
CookieUtil.getCookieMap(request)
CookieUtil.deleteCookie(request, "shopName")
特殊时候,由于Cookie原先保存时候设置了path
属性,那么我们需要如此这般
CookieEntity cookieEntity = new CookieEntity("shopName", "feilong");
cookieEntity.setPath("/member/account");
CookieUtil.deleteCookie(request, "shopName");