cookie工具

package com.other.helper;


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.StringUtils;


import com.other.PublicDefine;


/**
 * cookie常用操作工具类
 * @author Jackie
 * @since 2015-08-10
 * @version 1.0.0
 */
public class CookieUtil {

public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
Integer maxAge, String path, String domain, Boolean secure) {
try {
name = URLEncoder.encode(name, "UTF-8");
value = URLEncoder.encode(value, "UTF-8");
Cookie cookie = new Cookie(name, value);
if (maxAge != null) {
cookie.setMaxAge(maxAge.intValue());
}
if (StringUtils.isNotEmpty(path)) {
cookie.setPath(path);
}
if (StringUtils.isNotEmpty(domain)) {
cookie.setDomain(domain);
}
if (secure != null) {
cookie.setSecure(secure.booleanValue());
}
response.addCookie(cookie);
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
}
}


public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
Integer maxAge) {
addCookie(request, response, name, value, maxAge, "/", null, null);
}


public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) {
addCookie(request, response, name, value, null, null, null, null);
}


public static String getCookie(HttpServletRequest request, String name) {
Cookie[] arrayCookie = request.getCookies();
if (arrayCookie != null) {
try {
name = URLEncoder.encode(name, "UTF-8");
for (Cookie cookie : arrayCookie) {
if (name.equals(cookie.getName())) {
return URLDecoder.decode(cookie.getValue(), "UTF-8");
}
}
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
}
}
return null;
}


public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name, String path,
String domain) {
try {
name = URLEncoder.encode(name, "UTF-8");
Cookie cookie = new Cookie(name, null);
cookie.setMaxAge(0);
if (StringUtils.isNotEmpty(path)) {
cookie.setPath(path);
}
if (StringUtils.isNotEmpty(domain)) {
cookie.setDomain(domain);
}
response.addCookie(cookie);
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
}
}


public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name) {
// removeCookie(request, response, name, localSetting.getCookiePath(),
// localSetting.getCookieDomain());
removeCookie(request, response, name, "/", null);
}
//xiaolan version5  add
public static void locationRequest(HttpServletRequest request, HttpServletResponse response,String py,String name){
if(StringUtils.isNotEmpty(py)){
CookieUtil.addCookie(request, response, PublicDefine.DEFALUT_LOCALHOST_ID, py+":"+name, PublicDefine.DEFAULT_COOKIE_LIFECYCLE);
}
}
//xiaolan version5  add
public static String GetLocationCity(HttpServletRequest request) { 
String location = CookieUtil.getCookie(request, PublicDefine.DEFALUT_LOCALHOST_ID);
if( location != null && location != "" )
{
String [] list = location.split(":");
if (StringUtils.isNotEmpty(location) && (list.length == 2)) {
return list[0];
} else {
return "";
}
}
else
return ""; 

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值