java cookie类_java cookie 工具类

1 packagecom.xxx.xxx.xxx.xxx;2

3 importjava.net.URLDecoder;4 importjava.net.URLEncoder;5

6 importjavax.servlet.http.Cookie;7 importjavax.servlet.http.HttpServletRequest;8 importjavax.servlet.http.HttpServletResponse;9

10 importorg.apache.commons.lang.StringUtils;11 importorg.apache.http.cookie.SetCookie;12 importorg.slf4j.Logger;13 importorg.slf4j.LoggerFactory;14 importorg.springframework.web.bind.annotation.ResponseBody;15

16 /**

17 * Cookie 工具类18 *19 *@author

20 * @date21 */

22 public final classCookieUtils {23 protected static final Logger LOGGER =LoggerFactory24 .getLogger(CookieUtils.class);25

26 /**

27 * 获取cookie的值28 *29 *@author

30 * @date31 *@paramreq32 *@paramres33 *@paramcookieName34 *@paramisDecoder35 * 是:utf-8编码 否:不编码使用默认的36 *@return

37 */

38 public staticString getCookieValue(HttpServletRequest req, String cookieName, Boolean isDecoder) {39 Cookie[] cookieList =req.getCookies();40 if (cookieList == null || cookieName == null) {41 return null;42 }43 String retValueString = null;44 try{45 for (int i = 0; i < cookieList.length; i++) {46 if(cookieList[i].getName().equals(cookieName)) {47 if(isDecoder) {48 retValueString =URLDecoder.decode(49 cookieList[i].getValue(), "utf-8");50 } else{51 retValueString =cookieList[i].getValue();52 }53 }54 }55 } catch(Exception e) {56 LOGGER.error("Cookie Decode Error : ", e);57 }58 returnretValueString;59

60 }61

62 /**

63 * 设置cookie64 *65 *@author

66 * @date67 *@paramreq68 *@paramres69 *@paramcookieName70 *@paramcookieValue71 *@paramcookieMaxAge72 * 设置cookie最大生存时间 单位秒73 *@paramisDecoder74 * 是:utf-8编码 否:不编码使用默认的75 */

76 public static voidsetCookie(HttpServletRequest req,77 HttpServletResponse res, String cookieName, String cookieValue,78 intcookieMaxAge, Boolean isDecoder) {79 if (cookieValue == null) {80 LOGGER.info(cookieName + " 为 null");81 return;82 }83 try{84 if(isDecoder) {85 cookieValue = URLEncoder.encode(cookieValue, "utf-8");86 }87 Cookie cookie = newCookie(cookieName, cookieValue);88 if (cookieMaxAge > 0) {89 cookie.setMaxAge(cookieMaxAge);90 }91 if (req != null) {92 cookie.setDomain(getDomainName(req)); //设置cookie域名

93 }94 cookie.setPath("/");95 res.addCookie(cookie);96 } catch(Exception e) {97 LOGGER.error("Cookie Decode Error : ", e);98 }99

100 }101 /**

102 * 删除cookie103 *@author

104 * @date105 *@paramreq106 *@paramres107 *@paramcookieName108 */

109 public static voiddeleteCookie(HttpServletRequest req,HttpServletResponse res,String cookieName){110 setCookie(req, res, cookieName, "", 0, false);111 }112

113 /**

114 * 获取cookie作用域115 *@authorgaobing116 * @date 2016年11月23日 上午11:13:37117 *@paramreq118 *@return

119 */

120 private static finalString getDomainName(HttpServletRequest req) {121 String domainName = "";122 String serverName =req.getRequestURL().toString();123

124 //String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort();

125 if(StringUtils.isNotBlank(serverName)) {126 serverName = serverName.toLowerCase().substring(7);127 final int end = serverName.indexOf("/");128 serverName = serverName.substring(0, end);129 final String[] domains = serverName.split("\\.");//\\表示转义

130 int len =domains.length;131 if (len > 3) {132 //www.xxx.com.cn

133 domainName = "." + domains[len - 3] + "." + domains[len - 2]134 + "." + domains[len - 1];135 } else if (len <= 3 && len > 1) {136 //xxx.com or xxx.cn

137 domainName = "." + domains[len - 2] + "." + domains[len - 1];138 } else{139 domainName =serverName;140 }141 }142

143 if (domainName != "" && domainName.indexOf(":") > 0) {144 String[] ary = domainName.split("\\:");145 domainName = ary[0];146 }147 returndomainName;148 }149

150 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值