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 "";
}
}
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 "";
}
}